github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/testgrid/jenkins_verify/jenkins_validate.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 main
    18  
    19  import (
    20  	"fmt"
    21  	"io/ioutil"
    22  	"os"
    23  	"path/filepath"
    24  	"strings"
    25  
    26  	prow_config "k8s.io/test-infra/prow/config"
    27  	"k8s.io/test-infra/testgrid/config/yaml2proto"
    28  )
    29  
    30  func main() {
    31  	args := os.Args[1:]
    32  
    33  	if len(args) != 3 {
    34  		fmt.Println("Missing args - usage: go run jenkins_validate.go <path/to/job_collection> <path/to/prow> <path/to/testgrid_config>")
    35  		os.Exit(1)
    36  	}
    37  
    38  	jobPath := args[0]
    39  	prowPath := args[1]
    40  	configPath := args[2]
    41  
    42  	jobs := make(map[string]bool)
    43  	files, err := filepath.Glob(jobPath + "/*")
    44  	if err != nil {
    45  		fmt.Println("Failed to collect outputs.")
    46  		os.Exit(1)
    47  	}
    48  
    49  	for _, file := range files {
    50  		file = strings.TrimPrefix(file, jobPath+"/")
    51  		jobs[file] = false
    52  	}
    53  
    54  	data, err := ioutil.ReadFile(configPath)
    55  	if err != nil {
    56  		fmt.Printf("Failed reading %v\n", configPath)
    57  		os.Exit(1)
    58  	}
    59  
    60  	c := yaml2proto.Config{}
    61  	if err := c.Update(data); err != nil {
    62  		fmt.Printf("Failed to convert yaml to protobuf: %v\n", err)
    63  		os.Exit(1)
    64  	}
    65  
    66  	config, err := c.Raw()
    67  	if err != nil {
    68  		fmt.Printf("Error validating config: %v\n", err)
    69  		os.Exit(1)
    70  	}
    71  
    72  	prowConfig, err := prow_config.Load(prowPath + "/config.yaml")
    73  	if err != nil {
    74  		fmt.Printf("Could not load prow configs: %v\n", err)
    75  		os.Exit(1)
    76  	}
    77  
    78  	// Also check k/k presubmit, prow postsubmit and periodic jobs
    79  	for _, job := range prowConfig.AllPresubmits([]string{"kubernetes/kubernetes"}) {
    80  		jobs[job.Name] = false
    81  	}
    82  
    83  	for _, job := range prowConfig.AllPostsubmits([]string{}) {
    84  		if job.Agent != "jenkins" {
    85  			jobs[job.Name] = false
    86  		}
    87  	}
    88  
    89  	for _, job := range prowConfig.AllPeriodics() {
    90  		if job.Agent != "jenkins" {
    91  			jobs[job.Name] = false
    92  		}
    93  	}
    94  
    95  	// For now anything outsite k8s-jenkins/(pr-)logs are considered to be fine
    96  	testgroups := make(map[string]bool)
    97  	for _, testgroup := range config.TestGroups {
    98  		if strings.Contains(testgroup.GcsPrefix, "kubernetes-jenkins/logs/") {
    99  			job := strings.TrimPrefix(testgroup.GcsPrefix, "kubernetes-jenkins/logs/")
   100  			testgroups[job] = false
   101  		}
   102  
   103  		if strings.Contains(testgroup.GcsPrefix, "kubernetes-jenkins/pr-logs/directory/") {
   104  			job := strings.TrimPrefix(testgroup.GcsPrefix, "kubernetes-jenkins/pr-logs/directory/")
   105  			testgroups[job] = false
   106  		}
   107  	}
   108  
   109  	// Cross check
   110  	// -- Each job need to have a match testgrid group
   111  	for job := range jobs {
   112  		if _, ok := testgroups[job]; ok {
   113  			testgroups[job] = true
   114  			jobs[job] = true
   115  		}
   116  	}
   117  
   118  	// Conclusion
   119  	badjobs := []string{}
   120  	for job, valid := range jobs {
   121  		if !valid {
   122  			badjobs = append(badjobs, job)
   123  			fmt.Printf("Job %v does not have a matching testgrid testgroup\n", job)
   124  		}
   125  	}
   126  
   127  	badconfigs := []string{}
   128  	for testgroup, valid := range testgroups {
   129  		if !valid {
   130  			badconfigs = append(badconfigs, testgroup)
   131  			fmt.Printf("Testgrid group %v does not have a matching jenkins or prow job\n", testgroup)
   132  		}
   133  	}
   134  
   135  	if len(badconfigs) > 0 {
   136  		fmt.Printf("Total bad config(s) - %v\n", len(badconfigs))
   137  	}
   138  
   139  	if len(badjobs) > 0 {
   140  		fmt.Printf("Total bad job(s) - %v\n", len(badjobs))
   141  	}
   142  
   143  	if len(badconfigs) > 0 || len(badjobs) > 0 {
   144  		os.Exit(1)
   145  	}
   146  }