sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/pod-utils/downwardapi/jobspec_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 downwardapi
    18  
    19  import (
    20  	"os"
    21  	"reflect"
    22  	"testing"
    23  
    24  	prowapi "sigs.k8s.io/prow/pkg/apis/prowjobs/v1"
    25  )
    26  
    27  func TestEnvironmentForSpec(t *testing.T) {
    28  	var tests = []struct {
    29  		name     string
    30  		spec     JobSpec
    31  		expected map[string]string
    32  	}{
    33  		{
    34  			name: "periodic job",
    35  			spec: JobSpec{
    36  				Type:      prowapi.PeriodicJob,
    37  				Job:       "job-name",
    38  				BuildID:   "0",
    39  				ProwJobID: "prowjob",
    40  			},
    41  			expected: map[string]string{
    42  				"CI":          "true",
    43  				"JOB_NAME":    "job-name",
    44  				"BUILD_ID":    "0",
    45  				"PROW_JOB_ID": "prowjob",
    46  				"JOB_TYPE":    "periodic",
    47  				"JOB_SPEC":    `{"type":"periodic","job":"job-name","buildid":"0","prowjobid":"prowjob"}`,
    48  			},
    49  		},
    50  		{
    51  			name: "postsubmit job",
    52  			spec: JobSpec{
    53  				Type:      prowapi.PostsubmitJob,
    54  				Job:       "job-name",
    55  				BuildID:   "0",
    56  				ProwJobID: "prowjob",
    57  				Refs: &prowapi.Refs{
    58  					Org:     "org-name",
    59  					Repo:    "repo-name",
    60  					BaseRef: "base-ref",
    61  					BaseSHA: "base-sha",
    62  				},
    63  			},
    64  			expected: map[string]string{
    65  				"CI":            "true",
    66  				"JOB_NAME":      "job-name",
    67  				"BUILD_ID":      "0",
    68  				"PROW_JOB_ID":   "prowjob",
    69  				"JOB_TYPE":      "postsubmit",
    70  				"JOB_SPEC":      `{"type":"postsubmit","job":"job-name","buildid":"0","prowjobid":"prowjob","refs":{"org":"org-name","repo":"repo-name","base_ref":"base-ref","base_sha":"base-sha"}}`,
    71  				"REPO_OWNER":    "org-name",
    72  				"REPO_NAME":     "repo-name",
    73  				"PULL_BASE_REF": "base-ref",
    74  				"PULL_BASE_SHA": "base-sha",
    75  				"PULL_REFS":     "base-ref:base-sha",
    76  			},
    77  		},
    78  		{
    79  			name: "batch job",
    80  			spec: JobSpec{
    81  				Type:      prowapi.BatchJob,
    82  				Job:       "job-name",
    83  				BuildID:   "0",
    84  				ProwJobID: "prowjob",
    85  				Refs: &prowapi.Refs{
    86  					Org:     "org-name",
    87  					Repo:    "repo-name",
    88  					BaseRef: "base-ref",
    89  					BaseSHA: "base-sha",
    90  					Pulls: []prowapi.Pull{{
    91  						Number:  1,
    92  						Author:  "author-name",
    93  						SHA:     "pull-sha",
    94  						HeadRef: "branch-name-1",
    95  					}, {
    96  						Number:  2,
    97  						Author:  "other-author-name",
    98  						SHA:     "second-pull-sha",
    99  						HeadRef: "branch-name-2",
   100  					}},
   101  				},
   102  			},
   103  			expected: map[string]string{
   104  				"CI":            "true",
   105  				"JOB_NAME":      "job-name",
   106  				"BUILD_ID":      "0",
   107  				"PROW_JOB_ID":   "prowjob",
   108  				"JOB_TYPE":      "batch",
   109  				"JOB_SPEC":      `{"type":"batch","job":"job-name","buildid":"0","prowjobid":"prowjob","refs":{"org":"org-name","repo":"repo-name","base_ref":"base-ref","base_sha":"base-sha","pulls":[{"number":1,"author":"author-name","sha":"pull-sha","head_ref":"branch-name-1"},{"number":2,"author":"other-author-name","sha":"second-pull-sha","head_ref":"branch-name-2"}]}}`,
   110  				"REPO_OWNER":    "org-name",
   111  				"REPO_NAME":     "repo-name",
   112  				"PULL_BASE_REF": "base-ref",
   113  				"PULL_BASE_SHA": "base-sha",
   114  				"PULL_REFS":     "base-ref:base-sha,1:pull-sha,2:second-pull-sha",
   115  			},
   116  		},
   117  		{
   118  			name: "presubmit job",
   119  			spec: JobSpec{
   120  				Type:      prowapi.PresubmitJob,
   121  				Job:       "job-name",
   122  				BuildID:   "0",
   123  				ProwJobID: "prowjob",
   124  				Refs: &prowapi.Refs{
   125  					Org:     "org-name",
   126  					Repo:    "repo-name",
   127  					BaseRef: "base-ref",
   128  					BaseSHA: "base-sha",
   129  					Pulls: []prowapi.Pull{{
   130  						Number:  1,
   131  						Author:  "author-name",
   132  						SHA:     "pull-sha",
   133  						HeadRef: "branch-name",
   134  						Title:   "pull-title",
   135  					}},
   136  				},
   137  			},
   138  			expected: map[string]string{
   139  				"CI":            "true",
   140  				"JOB_NAME":      "job-name",
   141  				"BUILD_ID":      "0",
   142  				"PROW_JOB_ID":   "prowjob",
   143  				"JOB_TYPE":      "presubmit",
   144  				"JOB_SPEC":      `{"type":"presubmit","job":"job-name","buildid":"0","prowjobid":"prowjob","refs":{"org":"org-name","repo":"repo-name","base_ref":"base-ref","base_sha":"base-sha","pulls":[{"number":1,"author":"author-name","sha":"pull-sha","title":"pull-title","head_ref":"branch-name"}]}}`,
   145  				"REPO_OWNER":    "org-name",
   146  				"REPO_NAME":     "repo-name",
   147  				"PULL_BASE_REF": "base-ref",
   148  				"PULL_BASE_SHA": "base-sha",
   149  				"PULL_REFS":     "base-ref:base-sha,1:pull-sha",
   150  				"PULL_HEAD_REF": "branch-name",
   151  				"PULL_NUMBER":   "1",
   152  				"PULL_PULL_SHA": "pull-sha",
   153  				"PULL_TITLE":    "pull-title",
   154  			},
   155  		},
   156  		{
   157  			name: "kubernetes agent",
   158  			spec: JobSpec{
   159  				Type:      prowapi.PeriodicJob,
   160  				Job:       "job-name",
   161  				BuildID:   "0",
   162  				ProwJobID: "prowjob",
   163  				agent:     prowapi.KubernetesAgent,
   164  			},
   165  			expected: map[string]string{
   166  				"CI":           "true",
   167  				"JOB_NAME":     "job-name",
   168  				"BUILD_ID":     "0",
   169  				"PROW_JOB_ID":  "prowjob",
   170  				"BUILD_NUMBER": "0",
   171  				"JOB_TYPE":     "periodic",
   172  				"JOB_SPEC":     `{"type":"periodic","job":"job-name","buildid":"0","prowjobid":"prowjob"}`,
   173  			},
   174  		},
   175  		{
   176  			name: "jenkins agent",
   177  			spec: JobSpec{
   178  				Type:      prowapi.PeriodicJob,
   179  				Job:       "job-name",
   180  				BuildID:   "0",
   181  				ProwJobID: "prowjob",
   182  				agent:     prowapi.JenkinsAgent,
   183  			},
   184  			expected: map[string]string{
   185  				"CI":          "true",
   186  				"JOB_NAME":    "job-name",
   187  				"BUILD_ID":    "0",
   188  				"PROW_JOB_ID": "prowjob",
   189  				"JOB_TYPE":    "periodic",
   190  				"JOB_SPEC":    `{"type":"periodic","job":"job-name","buildid":"0","prowjobid":"prowjob"}`,
   191  			},
   192  		},
   193  	}
   194  
   195  	for _, test := range tests {
   196  		env, err := EnvForSpec(test.spec)
   197  		if err != nil {
   198  			t.Errorf("%s: unexpected error: %v", test.name, err)
   199  		}
   200  		if actual, expected := env, test.expected; !reflect.DeepEqual(actual, expected) {
   201  			t.Errorf("%s: got environment:\n\t%v\n\tbut expected:\n\t%v", test.name, actual, expected)
   202  		}
   203  	}
   204  }
   205  
   206  func TestGetRevisionFromSpec(t *testing.T) {
   207  	var tests = []struct {
   208  		name     string
   209  		spec     JobSpec
   210  		expected string
   211  	}{
   212  		{
   213  			name: "Refs with Pull",
   214  			spec: JobSpec{
   215  				Refs: &prowapi.Refs{
   216  					BaseRef: "master",
   217  					BaseSHA: "deadbeef",
   218  					Pulls: []prowapi.Pull{
   219  						{
   220  							Number: 123,
   221  							SHA:    "abcd1234",
   222  						},
   223  					},
   224  				},
   225  			},
   226  			expected: "abcd1234",
   227  		},
   228  		{
   229  			name: "Refs with BaseSHA",
   230  			spec: JobSpec{
   231  				Refs: &prowapi.Refs{
   232  					BaseRef: "master",
   233  					BaseSHA: "deadbeef",
   234  				},
   235  			},
   236  			expected: "deadbeef",
   237  		},
   238  		{
   239  			name: "Refs with BaseRef",
   240  			spec: JobSpec{
   241  				Refs: &prowapi.Refs{
   242  					BaseRef: "master",
   243  				},
   244  			},
   245  			expected: "master",
   246  		},
   247  		{
   248  			name: "Refs from extra_refs",
   249  			spec: JobSpec{
   250  				ExtraRefs: []prowapi.Refs{
   251  					{
   252  						BaseRef: "master",
   253  					},
   254  				},
   255  			},
   256  			expected: "master",
   257  		},
   258  	}
   259  
   260  	for _, test := range tests {
   261  		if actual, expected := GetRevisionFromSpec(&test.spec), test.expected; actual != expected {
   262  			t.Errorf("%s: got revision:%s but expected: %s", test.name, actual, expected)
   263  		}
   264  	}
   265  }
   266  
   267  func TestInCI(t *testing.T) {
   268  	os.Setenv("CI", "true")
   269  	if !InCI() {
   270  		t.Error("we expected InCI() to return true, but it returned false")
   271  	}
   272  }