github.com/GoogleContainerTools/skaffold/v2@v2.13.2/pkg/diag/diag_test.go (about)

     1  /*
     2  Copyright 2020 The Skaffold Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package diag
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"testing"
    23  
    24  	"github.com/google/go-cmp/cmp"
    25  	"google.golang.org/protobuf/testing/protocmp"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  
    28  	"github.com/GoogleContainerTools/skaffold/v2/pkg/diag/validator"
    29  	"github.com/GoogleContainerTools/skaffold/v2/testutil"
    30  )
    31  
    32  type mockValidator struct {
    33  	ns          []string
    34  	listOptions metav1.ListOptions
    35  }
    36  
    37  type mockErrValidator struct{}
    38  
    39  func (m *mockValidator) Validate(_ context.Context, ns string, opts metav1.ListOptions) ([]validator.Resource, error) {
    40  	m.ns = append(m.ns, ns)
    41  	m.listOptions = opts
    42  	return nil, nil
    43  }
    44  
    45  func (e *mockErrValidator) Validate(_ context.Context, ns string, opts metav1.ListOptions) ([]validator.Resource, error) {
    46  	return nil, fmt.Errorf("error")
    47  }
    48  
    49  func TestRun(t *testing.T) {
    50  	tests := []struct {
    51  		description string
    52  		labels      map[string]string
    53  		ns          []string
    54  		expected    *mockValidator
    55  	}{
    56  		{
    57  			description: "multiple namespaces with an empty namespace and no labels",
    58  			ns:          []string{"foo", "bar", ""},
    59  			expected: &mockValidator{
    60  				ns:          []string{"foo", "bar", ""},
    61  				listOptions: metav1.ListOptions{},
    62  			},
    63  		},
    64  		{
    65  			description: "empty namespaces no labels",
    66  			ns:          []string{""},
    67  			expected:    &mockValidator{ns: []string{""}},
    68  		},
    69  		{
    70  			description: "multiple namespaces and multiple labels",
    71  			ns:          []string{"foo", "goo"},
    72  			labels: map[string]string{
    73  				"skaffold":       "session",
    74  				"deployment-app": "app",
    75  			},
    76  			expected: &mockValidator{
    77  				ns: []string{"foo", "goo"},
    78  				listOptions: metav1.ListOptions{
    79  					LabelSelector: "deployment-app=app,skaffold=session",
    80  				},
    81  			},
    82  		},
    83  	}
    84  	for _, test := range tests {
    85  		testutil.Run(t, test.description, func(t *testutil.T) {
    86  			d := New(test.ns)
    87  			for k, v := range test.labels {
    88  				d = d.WithLabel(k, v)
    89  			}
    90  			m := &mockValidator{}
    91  			d = d.WithValidators([]validator.Validator{m})
    92  			d.Run(context.Background())
    93  			t.CheckDeepEqual(test.expected, m, cmp.AllowUnexported(mockValidator{}), protocmp.Transform())
    94  		})
    95  	}
    96  }
    97  
    98  func TestRunErr(t *testing.T) {
    99  	tests := []struct {
   100  		description    string
   101  		shouldErr      bool
   102  		labels         map[string]string
   103  		ns             []string
   104  		expectedErrMsg string
   105  	}{
   106  		{
   107  			description: "handles error",
   108  			shouldErr:   true,
   109  			labels: map[string]string{
   110  				"skaffold": "session",
   111  			},
   112  			ns:             []string{"foo"},
   113  			expectedErrMsg: "following errors occurred error\n",
   114  		},
   115  	}
   116  
   117  	for _, test := range tests {
   118  		testutil.Run(t, test.description, func(t *testutil.T) {
   119  			d := New(test.ns)
   120  			m := &mockErrValidator{}
   121  			d = d.WithValidators([]validator.Validator{m})
   122  			_, err := d.Run(context.Background())
   123  			t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expectedErrMsg, err.Error())
   124  		})
   125  	}
   126  }