github.com/zntrio/harp/v2@v2.0.9/pkg/tasks/container/identity_test.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package container 19 20 import ( 21 "context" 22 "errors" 23 "io" 24 "testing" 25 26 "github.com/zntrio/harp/v2/pkg/sdk/cmdutil" 27 "github.com/zntrio/harp/v2/pkg/sdk/value" 28 "github.com/zntrio/harp/v2/pkg/sdk/value/identity" 29 "github.com/zntrio/harp/v2/pkg/sdk/value/mock" 30 "github.com/zntrio/harp/v2/pkg/tasks" 31 ) 32 33 func TestIdentityTask_Run(t *testing.T) { 34 type fields struct { 35 OutputWriter tasks.WriterProvider 36 Description string 37 Transformer value.Transformer 38 Version IdentityVersion 39 } 40 type args struct { 41 ctx context.Context 42 } 43 tests := []struct { 44 name string 45 fields fields 46 args args 47 wantErr bool 48 }{ 49 { 50 name: "nil", 51 wantErr: true, 52 }, 53 { 54 name: "nil outputWriter", 55 fields: fields{ 56 OutputWriter: nil, 57 }, 58 wantErr: true, 59 }, 60 { 61 name: "nil transformer", 62 fields: fields{ 63 OutputWriter: cmdutil.DiscardWriter(), 64 Transformer: nil, 65 }, 66 wantErr: true, 67 }, 68 { 69 name: "blank description", 70 fields: fields{ 71 OutputWriter: cmdutil.DiscardWriter(), 72 Transformer: identity.Transformer(), 73 Description: "", 74 }, 75 wantErr: true, 76 }, 77 { 78 name: "transformer error", 79 fields: fields{ 80 OutputWriter: cmdutil.DiscardWriter(), 81 Transformer: mock.Transformer(errors.New("test")), 82 Description: "test", 83 }, 84 wantErr: true, 85 }, 86 { 87 name: "outputWriter error", 88 fields: fields{ 89 OutputWriter: func(ctx context.Context) (io.Writer, error) { 90 return nil, errors.New("test") 91 }, 92 Description: "test", 93 Transformer: identity.Transformer(), 94 }, 95 wantErr: true, 96 }, 97 { 98 name: "outputWriter closed", 99 fields: fields{ 100 OutputWriter: func(ctx context.Context) (io.Writer, error) { 101 return cmdutil.NewClosedWriter(), nil 102 }, 103 Description: "test", 104 Transformer: identity.Transformer(), 105 }, 106 wantErr: true, 107 }, 108 { 109 name: "version unspecified", 110 fields: fields{ 111 OutputWriter: cmdutil.DiscardWriter(), 112 Description: "test", 113 Transformer: identity.Transformer(), 114 }, 115 wantErr: true, 116 }, 117 // --------------------------------------------------------------------- 118 { 119 name: "valid - v1", 120 fields: fields{ 121 OutputWriter: cmdutil.DiscardWriter(), 122 Description: "test", 123 Transformer: identity.Transformer(), 124 Version: LegacyIdentity, 125 }, 126 wantErr: false, 127 }, 128 { 129 name: "valid - v2", 130 fields: fields{ 131 OutputWriter: cmdutil.DiscardWriter(), 132 Description: "test", 133 Transformer: identity.Transformer(), 134 Version: ModernIdentity, 135 }, 136 wantErr: false, 137 }, 138 { 139 name: "valid - v3", 140 fields: fields{ 141 OutputWriter: cmdutil.DiscardWriter(), 142 Description: "test", 143 Transformer: identity.Transformer(), 144 Version: NISTIdentity, 145 }, 146 wantErr: false, 147 }, 148 } 149 for _, tt := range tests { 150 t.Run(tt.name, func(t *testing.T) { 151 tr := &IdentityTask{ 152 OutputWriter: tt.fields.OutputWriter, 153 Description: tt.fields.Description, 154 Transformer: tt.fields.Transformer, 155 Version: tt.fields.Version, 156 } 157 if err := tr.Run(tt.args.ctx); (err != nil) != tt.wantErr { 158 t.Errorf("IdentityTask.Run() error = %v, wantErr %v", err, tt.wantErr) 159 } 160 }) 161 } 162 }