github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/gitlab/yaml/image_test.go (about) 1 // Copyright 2022 Harness, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package yaml 16 17 import ( 18 "testing" 19 20 "github.com/google/go-cmp/cmp" 21 "gopkg.in/yaml.v3" 22 ) 23 24 func TestImage(t *testing.T) { 25 tests := []struct { 26 yaml string 27 want Image 28 }{ 29 { 30 yaml: `"golang"`, 31 want: Image{ 32 Name: "golang", 33 }, 34 }, 35 { 36 yaml: `"golang:latest"`, 37 want: Image{ 38 Name: "golang:latest", 39 }, 40 }, 41 { 42 yaml: `"golang:1.19"`, 43 want: Image{ 44 Name: "golang:1.19", 45 }, 46 }, 47 { 48 yaml: `{ "name": "postgres:12", "alias": "postgres", "pull_policy": "always" }`, 49 want: Image{ 50 Name: "postgres:12", 51 Alias: "postgres", 52 PullPolicy: Stringorslice{"always"}, 53 }, 54 }, 55 { 56 yaml: `{ "name": "alpine:latest", "entrypoint": ["/bin/sh", "-c"] }`, 57 want: Image{ 58 Name: "alpine:latest", 59 Entrypoint: []string{"/bin/sh", "-c"}, 60 }, 61 }, 62 } 63 64 for i, test := range tests { 65 got := new(Image) 66 if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil { 67 t.Error(err) 68 return 69 } 70 if diff := cmp.Diff(got, &test.want); diff != "" { 71 t.Errorf("Unexpected parsing results for test %v", i) 72 t.Log(diff) 73 } 74 } 75 } 76 77 func TestImage_Error(t *testing.T) { 78 err := yaml.Unmarshal([]byte("[]"), new(Image)) 79 if err == nil || err.Error() != "failed to unmarshal image" { 80 t.Errorf("Expect error, got %s", err) 81 } 82 }