github.com/kubesphere/s2irun@v3.2.1+incompatible/pkg/api/types_test.go (about) 1 package api 2 3 import ( 4 "path/filepath" 5 "reflect" 6 "testing" 7 ) 8 9 func TestVolumeListSet(t *testing.T) { 10 table := []struct { 11 Input string 12 Expected VolumeList 13 }{ 14 {"/test:", VolumeList{{Source: "/test", Destination: "."}}}, 15 {"/test:/test", VolumeList{{Source: "/test", Destination: "/test"}}}, 16 {"/test/foo:/etc/ssl", VolumeList{{Source: "/test/foo", Destination: "/etc/ssl"}}}, 17 {":/foo", VolumeList{{Source: ".", Destination: "/foo"}}}, 18 {"/foo", VolumeList{{Source: "/foo", Destination: "."}}}, 19 {":", VolumeList{{Source: ".", Destination: "."}}}, 20 {"/t est/foo:", VolumeList{{Source: "/t est/foo", Destination: "."}}}, 21 {`"/test":"/foo"`, VolumeList{{Source: "/test", Destination: "/foo"}}}, 22 {`'/test':"/foo"`, VolumeList{{Source: "/test", Destination: "/foo"}}}, 23 {`C:\test:/bar`, VolumeList{{Source: `C:\test`, Destination: "/bar"}}}, 24 {`C:\test:bar`, VolumeList{{Source: `C:\test`, Destination: "bar"}}}, 25 {`"/te"st":"/foo"`, VolumeList{}}, 26 {"/test/foo:/ss;ss", VolumeList{ 27 {Source: "/test/foo", Destination: "/ss"}, 28 {Source: "ss", Destination: "."}, 29 }}, 30 {"/test;foo:/ssss", VolumeList{ 31 {Source: "/test", Destination: "."}, 32 {Source: "foo", Destination: "/ssss"}, 33 }}, 34 {"/test;foo:b@!dF1nl3m!", VolumeList{}}, 35 } 36 for _, test := range table { 37 if len(test.Expected) != 0 { 38 test.Expected[0].Source = filepath.FromSlash(test.Expected[0].Source) 39 } 40 got := VolumeList{} 41 got.Set(test.Input) 42 if !reflect.DeepEqual(got, test.Expected) { 43 t.Errorf("On test %s, got %#v, expected %#v", test.Input, got, test.Expected) 44 } 45 } 46 } 47 48 func TestEnvironmentSet(t *testing.T) { 49 table := map[string][]EnvironmentSpec{ 50 "FOO=bar": {{Name: "FOO", Value: "bar"}}, 51 "FOO=": {{Name: "FOO", Value: ""}}, 52 "FOO": {}, 53 "=": {}, 54 "FOO=bar,": {{Name: "FOO", Value: "bar,"}}, 55 // Users should get a deprecation warning in this case 56 // TODO: Create fake glog interface to be able to verify this. 57 "FOO=bar,BAR=foo": {{Name: "FOO", Value: "bar,BAR=foo"}}, 58 } 59 60 for v, expected := range table { 61 got := EnvironmentList{} 62 err := got.Set(v) 63 if len(expected) == 0 && err == nil { 64 t.Errorf("Expected error for env %q", v) 65 continue 66 } 67 if len(expected) != len(got) { 68 t.Errorf("got %d items, expected %d items in the list for %q", len(got), len(expected), v) 69 continue 70 } 71 for _, exp := range expected { 72 found := false 73 for _, g := range got { 74 if g.Name == exp.Name && g.Value == exp.Value { 75 found = true 76 break 77 } 78 } 79 if !found { 80 t.Errorf("Expected %+v environment found in %#v list", exp, got) 81 } 82 } 83 } 84 } 85 86 func TestGetFullImageName(t *testing.T) { 87 type runtest struct { 88 imageName string 89 serverAddress string 90 expected string 91 } 92 tests := []runtest{ 93 {"test/image", "test-harbor.io", "test-harbor.io/test/image:latest"}, 94 {"test/image:latest", "test-harbor.io", "test-harbor.io/test/image:latest"}, 95 {"test-harbor.io/test/image:tag", "test-harbor.io", "test-harbor.io/test/image:tag"}, 96 {"test/image:tag", "", "docker.io/test/image:tag"}, 97 {"test/image", "test-harbor.io:3333", "test-harbor.io:3333/test/image:latest"}, 98 {"test-harbor.io:3333/test/image", "", "test-harbor.io:3333/test/image:latest"}, 99 {"nginx", "docker.io", "docker.io/library/nginx:latest"}, 100 {"nginx:perl", "", "docker.io/library/nginx:perl"}, 101 {"127.0.0.1:5000/test:aaa", "127.0.0.1:5001", "127.0.0.1:5001/test:aaa"}, 102 {"127.0.0.1:5000/test:aaa", "", "127.0.0.1:5000/test:aaa"}, 103 {"nginx", "", "docker.io/library/nginx:latest"}, 104 {"zhuxiaoyang/nginx:v1", "docker.io", "docker.io/zhuxiaoyang/nginx:v1"}, 105 {"library/tomcat:v1", "http://harbor.devops.kubesphere.local:30280", "harbor.devops.kubesphere.local:30280/library/tomcat:v1"}, 106 {"library/tomcat:v1", "https://harbor.devops.kubesphere.local:30280", "harbor.devops.kubesphere.local:30280/library/tomcat:v1"}, 107 {"library/tomcat:v1", "harbor.devops.kubesphere.local:30280", "harbor.devops.kubesphere.local:30280/library/tomcat:v1"}, 108 {"zxytest/s2i-1:v1", "https://dockerhub.qingcloud.com", "dockerhub.qingcloud.com/zxytest/s2i-1:v1"}, 109 {"zxytest/s2i-1:v1", "http://dockerhub.qingcloud.com", "dockerhub.qingcloud.com/zxytest/s2i-1:v1"}, 110 {"zxytest/s2i-1:v1", "dockerhub.qingcloud.com", "dockerhub.qingcloud.com/zxytest/s2i-1:v1"}, 111 } 112 113 for _, tc := range tests { 114 if res, err := Parse(tc.imageName, tc.serverAddress); err != nil || res != tc.expected { 115 t.Errorf(tc.imageName) 116 t.Errorf("Expected image name %s, but got %s", tc.expected, res) 117 } 118 } 119 }