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