github.com/abayer/test-infra@v0.0.5/prow/pod-utils/gcs/target_test.go (about)

     1  /*
     2  Copyright 2017 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  package gcs
    18  
    19  import (
    20  	"testing"
    21  
    22  	"k8s.io/apimachinery/pkg/api/equality"
    23  
    24  	"k8s.io/test-infra/prow/kube"
    25  	"k8s.io/test-infra/prow/pod-utils/downwardapi"
    26  )
    27  
    28  func TestPathForSpec(t *testing.T) {
    29  	testCases := []struct {
    30  		name     string
    31  		spec     *downwardapi.JobSpec
    32  		builder  RepoPathBuilder
    33  		expected string
    34  	}{
    35  		{
    36  			name: "periodic",
    37  			spec: &downwardapi.JobSpec{
    38  				Type:    kube.PeriodicJob,
    39  				Job:     "job",
    40  				BuildID: "number",
    41  			},
    42  			expected: "logs/job/number",
    43  		},
    44  		{
    45  			name: "postsubmit",
    46  			spec: &downwardapi.JobSpec{Type: kube.PostsubmitJob,
    47  				Job:     "job",
    48  				BuildID: "number",
    49  			},
    50  			expected: "logs/job/number",
    51  		},
    52  		{
    53  			name: "batch",
    54  			spec: &downwardapi.JobSpec{Type: kube.BatchJob,
    55  				Job:     "job",
    56  				BuildID: "number",
    57  			},
    58  			expected: "pr-logs/pull/batch/job/number",
    59  		},
    60  		{
    61  			name: "presubmit full default legacy",
    62  			spec: &downwardapi.JobSpec{
    63  				Type:    kube.PresubmitJob,
    64  				Job:     "job",
    65  				BuildID: "number",
    66  				Refs: kube.Refs{
    67  					Org:  "org",
    68  					Repo: "repo",
    69  					Pulls: []kube.Pull{
    70  						{
    71  							Number: 1,
    72  						},
    73  					},
    74  				},
    75  			},
    76  			builder:  NewLegacyRepoPathBuilder("org", "repo"),
    77  			expected: "pr-logs/pull/1/job/number",
    78  		},
    79  		{
    80  			name: "presubmit default org legacy",
    81  			spec: &downwardapi.JobSpec{
    82  				Type:    kube.PresubmitJob,
    83  				Job:     "job",
    84  				BuildID: "number",
    85  				Refs: kube.Refs{
    86  					Org:  "org",
    87  					Repo: "repo",
    88  					Pulls: []kube.Pull{
    89  						{
    90  							Number: 1,
    91  						},
    92  					},
    93  				},
    94  			},
    95  			builder:  NewLegacyRepoPathBuilder("org", "other"),
    96  			expected: "pr-logs/pull/repo/1/job/number",
    97  		},
    98  		{
    99  			name: "presubmit nondefault legacy",
   100  			spec: &downwardapi.JobSpec{
   101  				Type:    kube.PresubmitJob,
   102  				Job:     "job",
   103  				BuildID: "number",
   104  				Refs: kube.Refs{
   105  					Org:  "org",
   106  					Repo: "repo",
   107  					Pulls: []kube.Pull{
   108  						{
   109  							Number: 1,
   110  						},
   111  					},
   112  				},
   113  			},
   114  			builder:  NewLegacyRepoPathBuilder("some", "other"),
   115  			expected: "pr-logs/pull/org_repo/1/job/number",
   116  		},
   117  	}
   118  
   119  	for _, test := range testCases {
   120  		if expected, actual := test.expected, PathForSpec(test.spec, test.builder); expected != actual {
   121  			t.Errorf("%s: expected path %q but got %q", test.name, expected, actual)
   122  		}
   123  	}
   124  }
   125  
   126  func TestAliasForSpec(t *testing.T) {
   127  	testCases := []struct {
   128  		name     string
   129  		spec     *downwardapi.JobSpec
   130  		expected string
   131  	}{
   132  		{
   133  			name:     "periodic",
   134  			spec:     &downwardapi.JobSpec{Type: kube.PeriodicJob},
   135  			expected: "",
   136  		},
   137  		{
   138  			name:     "batch",
   139  			spec:     &downwardapi.JobSpec{Type: kube.BatchJob},
   140  			expected: "",
   141  		},
   142  		{
   143  			name:     "postsubmit",
   144  			spec:     &downwardapi.JobSpec{Type: kube.PostsubmitJob},
   145  			expected: "",
   146  		},
   147  		{
   148  			name: "presubmit",
   149  			spec: &downwardapi.JobSpec{
   150  				Type:    kube.PresubmitJob,
   151  				Job:     "job",
   152  				BuildID: "number",
   153  			},
   154  			expected: "pr-logs/directory/job/number.txt",
   155  		},
   156  	}
   157  
   158  	for _, test := range testCases {
   159  		if expected, actual := test.expected, AliasForSpec(test.spec); expected != actual {
   160  			t.Errorf("%s: expected alias %q but got %q", test.name, expected, actual)
   161  		}
   162  	}
   163  }
   164  
   165  func TestLatestBuildForSpec(t *testing.T) {
   166  	testCases := []struct {
   167  		name     string
   168  		spec     *downwardapi.JobSpec
   169  		builder  RepoPathBuilder
   170  		expected []string
   171  	}{
   172  		{
   173  			name: "presubmit - no strategy",
   174  			spec: &downwardapi.JobSpec{
   175  				Type: kube.PresubmitJob,
   176  				Job:  "pull-kubernetes-unit",
   177  				Refs: kube.Refs{Org: "kubernetes", Repo: "test-infra", Pulls: []kube.Pull{{Number: 1234}}},
   178  			},
   179  			expected: []string{"pr-logs/directory/pull-kubernetes-unit/latest-build.txt"},
   180  		},
   181  		{
   182  			name: "presubmit - explicit strategy",
   183  			spec: &downwardapi.JobSpec{
   184  				Type: kube.PresubmitJob,
   185  				Job:  "pull-kubernetes-unit",
   186  				Refs: kube.Refs{Org: "kubernetes", Repo: "test-infra", Pulls: []kube.Pull{{Number: 1234}}},
   187  			},
   188  			builder: NewExplicitRepoPathBuilder(),
   189  			expected: []string{
   190  				"pr-logs/directory/pull-kubernetes-unit/latest-build.txt",
   191  				"pr-logs/pull/kubernetes_test-infra/1234/pull-kubernetes-unit/latest-build.txt",
   192  			},
   193  		},
   194  		{
   195  			name: "presubmit - legacy strategy",
   196  			spec: &downwardapi.JobSpec{
   197  				Type: kube.PresubmitJob,
   198  				Job:  "pull-kubernetes-unit",
   199  				Refs: kube.Refs{Org: "kubernetes", Repo: "test-infra", Pulls: []kube.Pull{{Number: 1234}}},
   200  			},
   201  			builder: NewLegacyRepoPathBuilder("kubernetes", "test-infra"),
   202  			expected: []string{
   203  				"pr-logs/directory/pull-kubernetes-unit/latest-build.txt",
   204  				"pr-logs/pull/1234/pull-kubernetes-unit/latest-build.txt",
   205  			},
   206  		},
   207  		{
   208  			name: "presubmit - single strategy",
   209  			spec: &downwardapi.JobSpec{
   210  				Type: kube.PresubmitJob,
   211  				Job:  "pull-kubernetes-unit",
   212  				Refs: kube.Refs{Org: "kubernetes", Repo: "test-infra", Pulls: []kube.Pull{{Number: 1234}}},
   213  			},
   214  			builder: NewSingleDefaultRepoPathBuilder("defaultorg", "defaultrepo"),
   215  			expected: []string{
   216  				"pr-logs/directory/pull-kubernetes-unit/latest-build.txt",
   217  				"pr-logs/pull/kubernetes_test-infra/1234/pull-kubernetes-unit/latest-build.txt",
   218  			},
   219  		},
   220  		{
   221  			name:     "batch",
   222  			spec:     &downwardapi.JobSpec{Type: kube.BatchJob, Job: "pull-kubernetes-unit"},
   223  			expected: []string{"pr-logs/directory/pull-kubernetes-unit/latest-build.txt"},
   224  		},
   225  		{
   226  			name:     "postsubmit",
   227  			spec:     &downwardapi.JobSpec{Type: kube.PostsubmitJob, Job: "ci-kubernetes-unit"},
   228  			expected: []string{"logs/ci-kubernetes-unit/latest-build.txt"},
   229  		},
   230  		{
   231  			name:     "periodic",
   232  			spec:     &downwardapi.JobSpec{Type: kube.PeriodicJob, Job: "ci-kubernetes-periodic"},
   233  			expected: []string{"logs/ci-kubernetes-periodic/latest-build.txt"},
   234  		},
   235  	}
   236  
   237  	for _, test := range testCases {
   238  		actual := LatestBuildForSpec(test.spec, test.builder)
   239  		if !equality.Semantic.DeepEqual(actual, test.expected) {
   240  			t.Errorf("%s: expected path %q but got %q", test.name, test.expected, actual)
   241  		}
   242  	}
   243  }
   244  
   245  func TestRootForSpec(t *testing.T) {
   246  	testCases := []struct {
   247  		name     string
   248  		spec     *downwardapi.JobSpec
   249  		expected string
   250  	}{
   251  		{
   252  			name:     "presubmit",
   253  			spec:     &downwardapi.JobSpec{Type: kube.PresubmitJob, Job: "pull-kubernetes-unit"},
   254  			expected: "pr-logs/directory/pull-kubernetes-unit",
   255  		},
   256  		{
   257  			name:     "batch",
   258  			spec:     &downwardapi.JobSpec{Type: kube.BatchJob, Job: "pull-kubernetes-unit"},
   259  			expected: "pr-logs/directory/pull-kubernetes-unit",
   260  		},
   261  		{
   262  			name:     "postsubmit",
   263  			spec:     &downwardapi.JobSpec{Type: kube.PostsubmitJob, Job: "ci-kubernetes-unit"},
   264  			expected: "logs/ci-kubernetes-unit",
   265  		},
   266  		{
   267  			name:     "periodic",
   268  			spec:     &downwardapi.JobSpec{Type: kube.PeriodicJob, Job: "ci-kubernetes-periodic"},
   269  			expected: "logs/ci-kubernetes-periodic",
   270  		},
   271  	}
   272  
   273  	for _, test := range testCases {
   274  		if expected, actual := test.expected, RootForSpec(test.spec); expected != actual {
   275  			t.Errorf("%s: expected path %q but got %q", test.name, expected, actual)
   276  		}
   277  	}
   278  }
   279  
   280  func TestNewLegacyRepoPathBuilder(t *testing.T) {
   281  	testCases := []struct {
   282  		name        string
   283  		defaultOrg  string
   284  		defaultRepo string
   285  		org         string
   286  		repo        string
   287  		expected    string
   288  	}{
   289  		{
   290  			name:        "default org and repo",
   291  			defaultOrg:  "org",
   292  			defaultRepo: "repo",
   293  			org:         "org",
   294  			repo:        "repo",
   295  			expected:    "",
   296  		},
   297  		{
   298  			name:        "default repo",
   299  			defaultOrg:  "org",
   300  			defaultRepo: "repo",
   301  			org:         "other",
   302  			repo:        "repo",
   303  			expected:    "other_repo",
   304  		},
   305  		{
   306  			name:        "default org",
   307  			defaultOrg:  "org",
   308  			defaultRepo: "repo",
   309  			org:         "org",
   310  			repo:        "other",
   311  			expected:    "other",
   312  		},
   313  		{
   314  			name:        "non-default",
   315  			defaultOrg:  "org",
   316  			defaultRepo: "repo",
   317  			org:         "other",
   318  			repo:        "wild",
   319  			expected:    "other_wild",
   320  		},
   321  	}
   322  
   323  	for _, test := range testCases {
   324  		builder := NewLegacyRepoPathBuilder(test.defaultOrg, test.defaultRepo)
   325  		if expected, actual := test.expected, builder(test.org, test.repo); expected != actual {
   326  			t.Errorf("%s: expected legacy repo path builder to create path segment %q but got %q", test.name, expected, actual)
   327  		}
   328  	}
   329  }
   330  
   331  func TestNewSingleDefaultRepoPathBuilder(t *testing.T) {
   332  	testCases := []struct {
   333  		name        string
   334  		defaultOrg  string
   335  		defaultRepo string
   336  		org         string
   337  		repo        string
   338  		expected    string
   339  	}{
   340  		{
   341  			name:        "default org and repo",
   342  			defaultOrg:  "org",
   343  			defaultRepo: "repo",
   344  			org:         "org",
   345  			repo:        "repo",
   346  			expected:    "",
   347  		},
   348  		{
   349  			name:        "default repo",
   350  			defaultOrg:  "org",
   351  			defaultRepo: "repo",
   352  			org:         "other",
   353  			repo:        "repo",
   354  			expected:    "other_repo",
   355  		},
   356  		{
   357  			name:        "default org",
   358  			defaultOrg:  "org",
   359  			defaultRepo: "repo",
   360  			org:         "org",
   361  			repo:        "other",
   362  			expected:    "org_other",
   363  		},
   364  		{
   365  			name:        "non-default",
   366  			defaultOrg:  "org",
   367  			defaultRepo: "repo",
   368  			org:         "other",
   369  			repo:        "wild",
   370  			expected:    "other_wild",
   371  		},
   372  	}
   373  
   374  	for _, test := range testCases {
   375  		builder := NewSingleDefaultRepoPathBuilder(test.defaultOrg, test.defaultRepo)
   376  		if expected, actual := test.expected, builder(test.org, test.repo); expected != actual {
   377  			t.Errorf("%s: expected single default repo path builder to create path segment %q but got %q", test.name, expected, actual)
   378  		}
   379  	}
   380  }
   381  
   382  func TestNewExplicitRepoPathBuilder(t *testing.T) {
   383  	if expected, actual := "a_b", NewExplicitRepoPathBuilder()("a", "b"); expected != actual {
   384  		t.Errorf("expected explicit repo path builder to create path segment %q but got %q", expected, actual)
   385  	}
   386  }