github.com/abayer/test-infra@v0.0.5/prow/deck/jobs/jobs_test.go (about)

     1  /*
     2  Copyright 2016 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 jobs
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"k8s.io/test-infra/prow/kube"
    24  )
    25  
    26  type fkc []kube.ProwJob
    27  
    28  func (f fkc) GetLog(pod string) ([]byte, error) {
    29  	return nil, nil
    30  }
    31  
    32  func (f fkc) ListPods(selector string) ([]kube.Pod, error) {
    33  	return nil, nil
    34  }
    35  
    36  func (f fkc) ListProwJobs(s string) ([]kube.ProwJob, error) {
    37  	return f, nil
    38  }
    39  
    40  type fpkc string
    41  
    42  func (f fpkc) GetContainerLog(pod, container string) ([]byte, error) {
    43  	if container != kube.TestContainerName {
    44  		return nil, fmt.Errorf("wrong container: %s", container)
    45  	}
    46  	if pod == "wowowow" || pod == "powowow" {
    47  		return []byte(f), nil
    48  	}
    49  	return nil, fmt.Errorf("pod not found: %s", pod)
    50  }
    51  
    52  func TestGetLog(t *testing.T) {
    53  	kc := fkc{
    54  		kube.ProwJob{
    55  			Spec: kube.ProwJobSpec{
    56  				Agent: kube.KubernetesAgent,
    57  				Job:   "job",
    58  			},
    59  			Status: kube.ProwJobStatus{
    60  				PodName: "wowowow",
    61  				BuildID: "123",
    62  			},
    63  		},
    64  		kube.ProwJob{
    65  			Spec: kube.ProwJobSpec{
    66  				Agent:   kube.KubernetesAgent,
    67  				Job:     "jib",
    68  				Cluster: "trusted",
    69  			},
    70  			Status: kube.ProwJobStatus{
    71  				PodName: "powowow",
    72  				BuildID: "123",
    73  			},
    74  		},
    75  	}
    76  	ja := &JobAgent{
    77  		kc:   kc,
    78  		pkcs: map[string]PodLogClient{kube.DefaultClusterAlias: fpkc("clusterA"), "trusted": fpkc("clusterB")},
    79  	}
    80  	if err := ja.update(); err != nil {
    81  		t.Fatalf("Updating: %v", err)
    82  	}
    83  	if res, err := ja.GetJobLog("job", "123"); err != nil {
    84  		t.Fatalf("Failed to get log: %v", err)
    85  	} else if got, expect := string(res), "clusterA"; got != expect {
    86  		t.Errorf("Unexpected result getting logs for job 'job'. Expected %q, but got %q.", expect, got)
    87  	}
    88  
    89  	if res, err := ja.GetJobLog("jib", "123"); err != nil {
    90  		t.Fatalf("Failed to get log: %v", err)
    91  	} else if got, expect := string(res), "clusterB"; got != expect {
    92  		t.Errorf("Unexpected result getting logs for job 'job'. Expected %q, but got %q.", expect, got)
    93  	}
    94  }
    95  
    96  func TestProwJobs(t *testing.T) {
    97  	kc := fkc{
    98  		kube.ProwJob{
    99  			Spec: kube.ProwJobSpec{
   100  				Agent: kube.KubernetesAgent,
   101  				Job:   "job",
   102  				Refs: &kube.Refs{
   103  					Org:  "kubernetes",
   104  					Repo: "test-infra",
   105  				},
   106  			},
   107  			Status: kube.ProwJobStatus{
   108  				PodName: "wowowow",
   109  				BuildID: "123",
   110  			},
   111  		},
   112  	}
   113  	ja := &JobAgent{
   114  		kc:   kc,
   115  		pkcs: map[string]PodLogClient{kube.DefaultClusterAlias: fpkc("")},
   116  	}
   117  	if err := ja.update(); err != nil {
   118  		t.Fatalf("Updating: %v", err)
   119  	}
   120  	pjs := ja.ProwJobs()
   121  	if expect, got := 1, len(pjs); expect != got {
   122  		t.Fatalf("Expected %d prowjobs, but got %d.", expect, got)
   123  	}
   124  	if expect, got := "kubernetes", pjs[0].Spec.Refs.Org; expect != got {
   125  		t.Errorf("Expected prowjob to have org %q, but got %q.", expect, got)
   126  	}
   127  }