github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/parser/parser_test.go (about) 1 // Copyright © 2021 Alibaba Group Holding Ltd. 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 parser 16 17 import ( 18 "reflect" 19 "testing" 20 21 "github.com/alibaba/sealer/version" 22 metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 24 v1 "github.com/alibaba/sealer/types/api/v1" 25 ) 26 27 func Test_decodeLine(t *testing.T) { 28 type args struct { 29 line string 30 } 31 tests := []struct { 32 name string 33 args args 34 want string 35 want1 string 36 wantErr bool 37 }{ 38 { 39 "test FROM command", 40 args{line: "FROM kubernetes:1.18.2"}, 41 "FROM", 42 "kubernetes:1.18.2", 43 false, 44 }, 45 { 46 "test FROM command", 47 args{line: " FROM kubernetes:1.18.2"}, 48 "FROM", 49 "kubernetes:1.18.2", 50 false, 51 }, 52 { 53 "test FROM command", 54 args{line: "FROM kubernetes:1.18.2 "}, 55 "FROM", 56 "kubernetes:1.18.2", 57 false, 58 }, 59 } 60 for _, tt := range tests { 61 t.Run(tt.name, func(t *testing.T) { 62 got, got1, err := decodeLine(tt.args.line) 63 if (err != nil) != tt.wantErr { 64 t.Errorf("decodeLine() error = %v, wantErr %v", err, tt.wantErr) 65 return 66 } 67 if got != tt.want { 68 t.Errorf("decodeLine() got = %v, want %v", got, tt.want) 69 } 70 if got1 != tt.want1 { 71 t.Errorf("decodeLine() got1 = %v, want %v", got1, tt.want1) 72 } 73 }) 74 } 75 } 76 77 func TestParser_Parse(t *testing.T) { 78 kubeFile := []byte(`FROM kubernetes:1.18.1 79 80 # this is annotations 81 COPY dashboard . 82 RUN echo "Config ssh ..." \ 83 && echo "PermitRootLogin yes" >> /etc/ssh/sshd_config 84 RUN kubectl apply -f dashboard`) 85 86 type args struct { 87 kubeFile []byte 88 name string 89 } 90 tests := []struct { 91 name string 92 args args 93 want *v1.Image 94 }{ 95 { 96 "test kubeFile", 97 args{ 98 kubeFile: kubeFile, 99 name: "", 100 }, 101 &v1.Image{ 102 TypeMeta: metaV1.TypeMeta{APIVersion: "", Kind: "Image"}, 103 Spec: v1.ImageSpec{ 104 SealerVersion: version.Get().GitVersion, 105 Layers: []v1.Layer{ 106 { 107 Type: "FROM", 108 Value: "kubernetes:1.18.1", 109 }, 110 { 111 Type: "COPY", 112 Value: "dashboard .", 113 }, 114 { 115 Type: "RUN", 116 Value: "echo \"Config ssh ...\" && echo \"PermitRootLogin yes\" >> /etc/ssh/sshd_config", 117 }, 118 { 119 Type: "RUN", 120 Value: "kubectl apply -f dashboard", 121 }, 122 }, 123 }, 124 }, 125 }, 126 } 127 for _, tt := range tests { 128 t.Run(tt.name, func(t *testing.T) { 129 p := &Parser{} 130 got, err := p.Parse(tt.args.kubeFile) 131 if err != nil { 132 t.Errorf("Parse() error = %v ", err) 133 return 134 } 135 if !reflect.DeepEqual(got, tt.want) { 136 t.Errorf("Parse() = %v, want %v", got, tt.want) 137 } 138 }) 139 } 140 }