| 
									
										
										
										
											2020-07-14 04:49:32 +08:00
										 |  |  | package util | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-01-21 21:29:21 +08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2021-09-28 05:25:06 +08:00
										 |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2020-07-14 04:49:32 +08:00
										 |  |  | 	"testing" | 
					
						
							| 
									
										
										
										
											2021-01-21 21:29:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-31 18:57:18 +08:00
										 |  |  | 	specs "github.com/opencontainers/runtime-spec/specs-go" | 
					
						
							| 
									
										
										
										
											2022-12-01 04:27:57 +08:00
										 |  |  | 	"github.com/stretchr/testify/assert" | 
					
						
							| 
									
										
										
										
											2025-08-29 20:55:12 +08:00
										 |  |  | 	"go.podman.io/common/pkg/config" | 
					
						
							| 
									
										
										
										
											2020-07-14 04:49:32 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestMergeEnv(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2025-02-04 13:36:03 +08:00
										 |  |  | 	t.Parallel() | 
					
						
							| 
									
										
										
										
											2020-07-14 04:49:32 +08:00
										 |  |  | 	tests := [][3][]string{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]string{"A=B", "B=C", "C=D"}, | 
					
						
							|  |  |  | 			nil, | 
					
						
							|  |  |  | 			[]string{"A=B", "B=C", "C=D"}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			nil, | 
					
						
							|  |  |  | 			[]string{"A=B", "B=C", "C=D"}, | 
					
						
							|  |  |  | 			[]string{"A=B", "B=C", "C=D"}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			[]string{"A=B", "B=C", "C=D", "E=F"}, | 
					
						
							|  |  |  | 			[]string{"B=O", "F=G"}, | 
					
						
							|  |  |  | 			[]string{"A=B", "B=O", "C=D", "E=F", "F=G"}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i, test := range tests { | 
					
						
							| 
									
										
										
										
											2021-09-28 05:25:06 +08:00
										 |  |  | 		t.Run(strconv.Itoa(i), func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-07-14 04:49:32 +08:00
										 |  |  | 			result := MergeEnv(test[0], test[1]) | 
					
						
							|  |  |  | 			if len(result) != len(test[2]) { | 
					
						
							|  |  |  | 				t.Fatalf("expected %v, got %v", test[2], result) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			for i := range result { | 
					
						
							|  |  |  | 				if result[i] != test[2][i] { | 
					
						
							|  |  |  | 					t.Fatalf("expected %v, got %v", test[2], result) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-03-31 18:57:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-21 21:29:21 +08:00
										 |  |  | func TestRuntime(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2025-02-04 13:36:03 +08:00
										 |  |  | 	t.Parallel() | 
					
						
							| 
									
										
										
										
											2021-01-21 21:29:21 +08:00
										 |  |  | 	os.Setenv("CONTAINERS_CONF", "/dev/null") | 
					
						
							|  |  |  | 	conf, _ := config.Default() | 
					
						
							|  |  |  | 	defaultRuntime := conf.Engine.OCIRuntime | 
					
						
							|  |  |  | 	runtime := Runtime() | 
					
						
							|  |  |  | 	if runtime != defaultRuntime { | 
					
						
							|  |  |  | 		t.Fatalf("expected %v, got %v", runtime, defaultRuntime) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	defaultRuntime = "myoci" | 
					
						
							|  |  |  | 	os.Setenv("BUILDAH_RUNTIME", defaultRuntime) | 
					
						
							|  |  |  | 	runtime = Runtime() | 
					
						
							|  |  |  | 	if runtime != defaultRuntime { | 
					
						
							|  |  |  | 		t.Fatalf("expected %v, got %v", runtime, defaultRuntime) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-03-31 18:57:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestMountsSort(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2025-02-04 13:36:03 +08:00
										 |  |  | 	t.Parallel() | 
					
						
							| 
									
										
										
										
											2021-03-31 18:57:18 +08:00
										 |  |  | 	mounts1a := []specs.Mount{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Source:      "/a/bb/c", | 
					
						
							|  |  |  | 			Destination: "/a/bb/c", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Source:      "/a/b/c", | 
					
						
							|  |  |  | 			Destination: "/a/b/c", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Source:      "/a", | 
					
						
							|  |  |  | 			Destination: "/a", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Source:      "/a/b", | 
					
						
							|  |  |  | 			Destination: "/a/b", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Source:      "/d/e", | 
					
						
							|  |  |  | 			Destination: "/a/c", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Source:      "/b", | 
					
						
							|  |  |  | 			Destination: "/b", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Source:      "/", | 
					
						
							|  |  |  | 			Destination: "/", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Source:      "/a/b/c", | 
					
						
							|  |  |  | 			Destination: "/aa/b/c", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-12-01 04:27:57 +08:00
										 |  |  | 	mounts1b := []string{ | 
					
						
							|  |  |  | 		"/", | 
					
						
							|  |  |  | 		"/a", | 
					
						
							|  |  |  | 		"/b", | 
					
						
							|  |  |  | 		"/a/b", | 
					
						
							|  |  |  | 		"/a/c", | 
					
						
							|  |  |  | 		"/a/b/c", | 
					
						
							|  |  |  | 		"/a/bb/c", | 
					
						
							|  |  |  | 		"/aa/b/c", | 
					
						
							| 
									
										
										
										
											2021-03-31 18:57:18 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	sorted := SortMounts(mounts1a) | 
					
						
							| 
									
										
										
										
											2022-12-01 04:27:57 +08:00
										 |  |  | 	sortedDests := make([]string, len(mounts1a)) | 
					
						
							| 
									
										
										
										
											2021-03-31 18:57:18 +08:00
										 |  |  | 	for i := range sorted { | 
					
						
							| 
									
										
										
										
											2022-12-01 04:27:57 +08:00
										 |  |  | 		sortedDests[i] = sorted[i].Destination | 
					
						
							| 
									
										
										
										
											2021-03-31 18:57:18 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-12-01 04:27:57 +08:00
										 |  |  | 	assert.Equalf(t, mounts1b, sortedDests, "sort returned results in unexpected by-destination order") | 
					
						
							| 
									
										
										
										
											2021-03-31 18:57:18 +08:00
										 |  |  | } |