github.com/SupersunnySea/draft@v0.16.0/pkg/local/local_test.go (about) 1 package local 2 3 import ( 4 "path/filepath" 5 "reflect" 6 "testing" 7 8 "k8s.io/api/core/v1" 9 ) 10 11 func TestDeployedApplication(t *testing.T) { 12 expectedApp := &App{ 13 Name: "example-app", 14 Namespace: "example-namespace", 15 } 16 17 app, err := DeployedApplication(filepath.Join("testdata", "draft.toml"), "development") 18 if err != nil { 19 t.Fatal(err) 20 } 21 22 if !reflect.DeepEqual(expectedApp, app) { 23 t.Errorf("Expected %#v, got %#v", expectedApp, app) 24 } 25 } 26 27 func TestGetTargetContainerPort(t *testing.T) { 28 containersTest1 := []v1.Container{ 29 {Name: "anothercontainer", Ports: []v1.ContainerPort{{ContainerPort: 3000}}}, 30 {Name: "mycontainer", Ports: []v1.ContainerPort{{ContainerPort: 4000}}}, 31 {Name: "multi-port", Ports: []v1.ContainerPort{{ContainerPort: 80}, {ContainerPort: 81}}}, 32 {Name: "no-port", Ports: []v1.ContainerPort{{}}}, 33 } 34 35 testCases := []struct { 36 description string 37 containers []v1.Container 38 targetContainer string 39 expectedPorts []int 40 expectErr bool 41 }{ 42 {"test correct container and port found", containersTest1, "mycontainer", []int{4000}, false}, 43 {"test container not found error", containersTest1, "randomcontainer", []int{0}, true}, 44 {"container found, multiple ports", containersTest1, "multi-port", []int{80, 81}, false}, 45 {"container found, no ports", containersTest1, "no-port", []int{0}, false}, 46 } 47 48 for _, tc := range testCases { 49 ports, err := getTargetContainerPorts(tc.containers, tc.targetContainer) 50 if tc.expectErr && err == nil { 51 t.Errorf("Expected err but did not get one for case: %s", tc.description) 52 } 53 54 if (!areEqual(tc.expectedPorts, []int{0})) && (!areEqual(tc.expectedPorts, ports)) { 55 t.Errorf("Expected port %v, got %v for scenario: %v", tc.expectedPorts, ports, tc.description) 56 } 57 } 58 } 59 60 func TestGetPortMapping(t *testing.T) { 61 testCases := []struct { 62 description string 63 portMapping []string 64 expectedMapping map[int]int 65 expectedErr bool 66 }{ 67 {"single port mapping", []string{"8080:8080"}, map[int]int{8080: 8080}, false}, 68 {"multiple port mappings", []string{"8080:80", "8081:81"}, map[int]int{80: 8080, 81: 8081}, false}, 69 {"multiple port mappings, same local port", []string{"8080:8080", "8080:8081"}, map[int]int{}, true}, 70 {"multiple port mappings, same remote port", []string{"8081:8081", "8080:8081"}, map[int]int{}, true}, 71 } 72 73 for _, tc := range testCases { 74 m, err := getPortMapping(tc.portMapping) 75 76 if tc.expectedErr { 77 if err == nil { 78 t.Errorf("Expected err but did not get one for test case: %s", tc.description) 79 } 80 } else { 81 if !reflect.DeepEqual(m, tc.expectedMapping) { 82 t.Errorf("For scenario %v - expected: %v, actual: %v", tc.description, tc.expectedMapping, m) 83 } 84 } 85 } 86 } 87 88 func areEqual(a, b []int) bool { 89 90 if a == nil && b == nil { 91 return true 92 } 93 94 if a == nil || b == nil { 95 return false 96 } 97 98 if len(a) != len(b) { 99 return false 100 } 101 102 for i := range a { 103 if a[i] != b[i] { 104 return false 105 } 106 } 107 108 return true 109 }