github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/internal/fnruntime/container_test.go (about)

     1  //go:build docker
     2  
     3  // Copyright 2021 Google LLC
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may 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, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package fnruntime
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"testing"
    23  
    24  	"github.com/GoogleContainerTools/kpt/internal/printer"
    25  	fnresult "github.com/GoogleContainerTools/kpt/pkg/api/fnresult/v1"
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  func TestContainerFn(t *testing.T) {
    31  	var tests = []struct {
    32  		input  string
    33  		image  string
    34  		output string
    35  		name   string
    36  		err    bool
    37  	}{
    38  		{
    39  			name:  "simple busybox",
    40  			image: "gcr.io/google-containers/busybox",
    41  		},
    42  		{
    43  			name:  "non-existing image",
    44  			image: "foobar",
    45  			err:   true,
    46  		},
    47  	}
    48  
    49  	for _, tt := range tests {
    50  		tt := tt
    51  		ctx := context.Background()
    52  		t.Run(tt.name, func(t *testing.T) {
    53  			errBuff := &bytes.Buffer{}
    54  			instance := ContainerFn{
    55  				Ctx:   printer.WithContext(ctx, printer.New(nil, errBuff)),
    56  				Image: tt.image,
    57  				FnResult: &fnresult.Result{
    58  					Image: tt.image,
    59  				},
    60  			}
    61  			input := bytes.NewBufferString(tt.input)
    62  			output := &bytes.Buffer{}
    63  			err := instance.Run(input, output)
    64  			if tt.err && !assert.Error(t, err) {
    65  				t.FailNow()
    66  			}
    67  			if !tt.err && !assert.NoError(t, err) {
    68  				t.FailNow()
    69  			}
    70  			if !assert.Equal(t, tt.output, output.String()) {
    71  				t.FailNow()
    72  			}
    73  		})
    74  	}
    75  }
    76  
    77  func TestIsSupportedDockerVersion(t *testing.T) {
    78  	tests := []struct {
    79  		name   string
    80  		inputV string
    81  		errMsg string
    82  	}{
    83  		{
    84  			name:   "greater than min version",
    85  			inputV: "20.10.1",
    86  		},
    87  		{
    88  			name:   "equal to min version",
    89  			inputV: "20.10.0",
    90  		},
    91  		{
    92  			name:   "less than min version",
    93  			inputV: "20.9.1",
    94  			errMsg: "docker client version must be v20.10.0 or greater: found v20.9.1",
    95  		},
    96  		{
    97  			name:   "invalid semver",
    98  			inputV: "20..12.1",
    99  			errMsg: "docker client version must be v20.10.0 or greater: found invalid version v20..12.1",
   100  		},
   101  	}
   102  	for _, tt := range tests {
   103  		tt := tt
   104  		t.Run(tt.name, func(t *testing.T) {
   105  			require := require.New(t)
   106  			err := isSupportedDockerVersion(tt.inputV)
   107  			if tt.errMsg != "" {
   108  				require.NotNil(err)
   109  				require.Contains(err.Error(), tt.errMsg)
   110  			} else {
   111  				require.NoError(err)
   112  			}
   113  		})
   114  	}
   115  }