github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/testgrid/cmd/configurator/yaml2proto_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 main
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func TestYaml2Proto_IsExternal_And_UseKuberClient_False(t *testing.T) {
    24  	yaml :=
    25  		`default_test_group:
    26    name: default
    27  default_dashboard_tab:
    28    name: default
    29  test_groups:
    30  - name: testgroup_1
    31  dashboards:
    32  - name: dashboard_1`
    33  
    34  	c := Config{}
    35  	err := c.Update([]byte(yaml))
    36  
    37  	if err != nil {
    38  		t.Errorf("Convert Error: %v\n", err)
    39  	}
    40  
    41  	config, err := c.Raw()
    42  	if err != nil {
    43  		t.Errorf("unexpected error: %v", err)
    44  		t.FailNow()
    45  	}
    46  	for _, testgroup := range config.TestGroups {
    47  		if !testgroup.IsExternal {
    48  			t.Errorf("IsExternal should always be true!")
    49  		}
    50  
    51  		if !testgroup.UseKubernetesClient {
    52  			t.Errorf("UseKubernetesClient should always be true!")
    53  		}
    54  	}
    55  }
    56  
    57  func TestUpdateDefaults_Validity(t *testing.T) {
    58  	tests := []struct {
    59  		yaml            string
    60  		expectedMissing string
    61  	}{
    62  		{
    63  			yaml:            ``,
    64  			expectedMissing: "DefaultTestGroup",
    65  		},
    66  		{
    67  			yaml: `default_test_group:
    68    name: default`,
    69  			expectedMissing: "DefaultDashboardTab",
    70  		},
    71  		{
    72  			yaml: `default_test_group:
    73    name: default
    74  default_dashboard_tab:
    75    name: default`,
    76  			expectedMissing: "",
    77  		},
    78  	}
    79  
    80  	for index, test := range tests {
    81  		c := Config{}
    82  		err := c.Update([]byte(test.yaml))
    83  		if err == nil && test.expectedMissing == "" {
    84  			continue
    85  		}
    86  		if err != nil {
    87  			if e, ok := err.(MissingFieldError); ok && e.Field == test.expectedMissing {
    88  				continue
    89  			}
    90  		}
    91  		t.Errorf("Test %v fails. expected MissingFieldError(%s), actual error: %v", index, test.expectedMissing, err)
    92  	}
    93  }
    94  func TestUpdate_Validate(t *testing.T) {
    95  	defaultYaml := `default_test_group:
    96    name: default
    97  default_dashboard_tab:
    98    name: default`
    99  
   100  	tests := []struct {
   101  		yaml            string
   102  		expectedMissing string
   103  	}{
   104  		{
   105  			yaml:            ``,
   106  			expectedMissing: "TestGroups",
   107  		},
   108  		{
   109  			yaml: `dashboards:
   110  - name: dashboard_1`,
   111  			expectedMissing: "TestGroups",
   112  		},
   113  		{
   114  			yaml: `test_groups:
   115  - name: testgroup_1`,
   116  			expectedMissing: "Dashboards",
   117  		},
   118  		{
   119  			yaml: `dashboards:
   120  - name: dashboard_1
   121  test_groups:
   122  - name: testgroup_1`,
   123  			expectedMissing: "",
   124  		},
   125  	}
   126  
   127  	for index, test := range tests {
   128  		c := Config{}
   129  		if err := c.Update([]byte(defaultYaml)); err != nil {
   130  			t.Errorf("Unexpected error in Update(defaultYaml): %v", err)
   131  		}
   132  		if err := c.Update([]byte(test.yaml)); err != nil {
   133  			t.Errorf("Unexpected error in Update(test[%d].yaml): %v", index, err)
   134  		}
   135  		err := c.validate()
   136  		if err == nil && test.expectedMissing == "" {
   137  			continue
   138  		}
   139  		if err != nil {
   140  			if e, ok := err.(MissingFieldError); ok && e.Field == test.expectedMissing {
   141  				continue
   142  			}
   143  		}
   144  		t.Errorf("Test %v fails. expected MissingFieldError(%s), actual error: %v", index, test.expectedMissing, err)
   145  	}
   146  }