github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/experiment/ci-janitor/main_test.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes 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  // ci-janitor cleans up dedicated projects in k8s prowjob configs
    18  package main
    19  
    20  import (
    21  	"testing"
    22  
    23  	buildv1alpha1 "github.com/knative/build/pkg/apis/build/v1alpha1"
    24  	"k8s.io/api/core/v1"
    25  	"k8s.io/apimachinery/pkg/api/equality"
    26  	"k8s.io/apimachinery/pkg/util/diff"
    27  
    28  	"k8s.io/test-infra/prow/config"
    29  )
    30  
    31  func TestContainers(t *testing.T) {
    32  	cases := []struct {
    33  		name     string
    34  		jb       config.JobBase
    35  		expected []v1.Container
    36  	}{
    37  		{
    38  			name: "only podspec",
    39  			jb: config.JobBase{
    40  				Spec: &v1.PodSpec{
    41  					Containers: []v1.Container{
    42  						{
    43  							Name: "hello",
    44  						},
    45  						{
    46  							Image: "there",
    47  						},
    48  					},
    49  				},
    50  			},
    51  			expected: []v1.Container{
    52  				{
    53  					Name: "hello",
    54  				},
    55  				{
    56  					Image: "there",
    57  				},
    58  			},
    59  		},
    60  		{
    61  			name: "only buildspec",
    62  			jb: config.JobBase{
    63  				BuildSpec: &buildv1alpha1.BuildSpec{
    64  					Steps: []v1.Container{
    65  						{
    66  							Command: []string{"hiya", "stranger"},
    67  						},
    68  						{
    69  							Args: []string{"fancy", "meeting", "you", "here"},
    70  						},
    71  					},
    72  					Source: &buildv1alpha1.SourceSpec{
    73  						Custom: &v1.Container{
    74  							WorkingDir: "so clone",
    75  						},
    76  					},
    77  				},
    78  			},
    79  			expected: []v1.Container{
    80  				{
    81  					Command: []string{"hiya", "stranger"},
    82  				},
    83  				{
    84  					Args: []string{"fancy", "meeting", "you", "here"},
    85  				},
    86  				{
    87  					WorkingDir: "so clone",
    88  				},
    89  			},
    90  		},
    91  		{
    92  			name: "build and pod specs",
    93  			jb: config.JobBase{
    94  				BuildSpec: &buildv1alpha1.BuildSpec{
    95  					Steps: []v1.Container{
    96  						{
    97  							Command: []string{"hiya", "stranger"},
    98  						},
    99  						{
   100  							Args: []string{"fancy", "meeting", "you", "here"},
   101  						},
   102  					},
   103  					Source: &buildv1alpha1.SourceSpec{
   104  						Custom: &v1.Container{
   105  							WorkingDir: "so clone",
   106  						},
   107  					},
   108  				},
   109  				Spec: &v1.PodSpec{
   110  					Containers: []v1.Container{
   111  						{
   112  							Name: "hello",
   113  						},
   114  						{
   115  							Image: "there",
   116  						},
   117  					},
   118  				},
   119  			},
   120  			expected: []v1.Container{
   121  				{
   122  					Name: "hello",
   123  				},
   124  				{
   125  					Image: "there",
   126  				},
   127  				{
   128  					Command: []string{"hiya", "stranger"},
   129  				},
   130  				{
   131  					Args: []string{"fancy", "meeting", "you", "here"},
   132  				},
   133  				{
   134  					WorkingDir: "so clone",
   135  				},
   136  			},
   137  		},
   138  	}
   139  
   140  	for _, tc := range cases {
   141  		t.Run(tc.name, func(t *testing.T) {
   142  			actual := containers(tc.jb)
   143  			if !equality.Semantic.DeepEqual(actual, tc.expected) {
   144  				t.Errorf("containers do not match:\n%s", diff.ObjectReflectDiff(tc.expected, actual))
   145  			}
   146  		})
   147  	}
   148  }