...

Source file src/k8s.io/kubectl/pkg/explain/model_printer_test.go

Documentation: k8s.io/kubectl/pkg/explain

     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 explain
    18  
    19  import (
    20  	"bytes"
    21  	"testing"
    22  
    23  	"k8s.io/apimachinery/pkg/runtime/schema"
    24  )
    25  
    26  func TestModel(t *testing.T) {
    27  	oneKind := schema.GroupVersionKind{
    28  		Group:   "",
    29  		Version: "v1",
    30  		Kind:    "OneKind",
    31  	}
    32  
    33  	controlCharacterKind := schema.GroupVersionKind{
    34  		Group:   "",
    35  		Version: "v1",
    36  		Kind:    "ControlCharacterKind",
    37  	}
    38  
    39  	tests := []struct {
    40  		path []string
    41  		want string
    42  		gvk  schema.GroupVersionKind
    43  	}{
    44  		{
    45  			want: `KIND:     OneKind
    46  VERSION:  v1
    47  
    48  DESCRIPTION:
    49       OneKind has a short description
    50  
    51  FIELDS:
    52     field1	<Object> -required-
    53       Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla ut lacus ac
    54       enim vulputate imperdiet ac accumsan risus. Integer vel accumsan lectus.
    55       Praesent tempus nulla id tortor luctus, quis varius nulla laoreet. Ut orci
    56       nisi, suscipit id velit sed, blandit eleifend turpis. Curabitur tempus ante
    57       at lectus viverra, a mattis augue euismod. Morbi quam ligula, porttitor sit
    58       amet lacus non, interdum pulvinar tortor. Praesent accumsan risus et ipsum
    59       dictum, vel ullamcorper lorem egestas.
    60  
    61     field2	<[]map[string]string>
    62       This is an array of object of PrimitiveDef
    63  
    64  `,
    65  			path: []string{},
    66  			gvk:  oneKind,
    67  		},
    68  		{
    69  			want: `KIND:     OneKind
    70  VERSION:  v1
    71  
    72  RESOURCE: field1 <Object>
    73  
    74  DESCRIPTION:
    75       Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla ut lacus ac
    76       enim vulputate imperdiet ac accumsan risus. Integer vel accumsan lectus.
    77       Praesent tempus nulla id tortor luctus, quis varius nulla laoreet. Ut orci
    78       nisi, suscipit id velit sed, blandit eleifend turpis. Curabitur tempus ante
    79       at lectus viverra, a mattis augue euismod. Morbi quam ligula, porttitor sit
    80       amet lacus non, interdum pulvinar tortor. Praesent accumsan risus et ipsum
    81       dictum, vel ullamcorper lorem egestas.
    82  
    83       This is another kind of Kind
    84  
    85  FIELDS:
    86     array	<[]integer>
    87       This array must be an array of int
    88  
    89     int	<integer>
    90       This int must be an int
    91  
    92     object	<map[string]string>
    93       This is an object of string
    94  
    95     primitive	<string>
    96  
    97     string	<string> -required-
    98       This string must be a string
    99  
   100  `,
   101  			path: []string{"field1"},
   102  			gvk:  oneKind,
   103  		},
   104  		{
   105  			want: `KIND:     OneKind
   106  VERSION:  v1
   107  
   108  FIELD:    string <string>
   109  
   110  DESCRIPTION:
   111       This string must be a string
   112  `,
   113  			path: []string{"field1", "string"},
   114  			gvk:  oneKind,
   115  		},
   116  		{
   117  			want: `KIND:     OneKind
   118  VERSION:  v1
   119  
   120  FIELD:    array <[]integer>
   121  
   122  DESCRIPTION:
   123       This array must be an array of int
   124  
   125       This is an int in an array
   126  `,
   127  			path: []string{"field1", "array"},
   128  			gvk:  oneKind,
   129  		},
   130  		{
   131  			want: `KIND:     ControlCharacterKind
   132  VERSION:  v1
   133  
   134  DESCRIPTION:
   135       Control character %
   136  
   137  FIELDS:
   138     field1	<>
   139       Control character %
   140  
   141  `,
   142  			path: []string{},
   143  			gvk:  controlCharacterKind,
   144  		},
   145  	}
   146  
   147  	for _, test := range tests {
   148  		schema := resources.LookupResource(test.gvk)
   149  		if schema == nil {
   150  			t.Fatalf("Couldn't find schema %v", test.gvk)
   151  		}
   152  		buf := bytes.Buffer{}
   153  		if err := PrintModelDescription(test.path, &buf, schema, test.gvk, false); err != nil {
   154  			t.Fatalf("Failed to PrintModelDescription for path %v: %v", test.path, err)
   155  		}
   156  		got := buf.String()
   157  		if got != test.want {
   158  			t.Errorf("Got:\n%v\nWant:\n%v\n", buf.String(), test.want)
   159  		}
   160  	}
   161  }
   162  func TestCRDModel(t *testing.T) {
   163  	gvk := schema.GroupVersionKind{
   164  		Group:   "",
   165  		Version: "v1",
   166  		Kind:    "CrdKind",
   167  	}
   168  	schema := resources.LookupResource(gvk)
   169  	if schema == nil {
   170  		t.Fatal("Couldn't find schema v1.CrdKind")
   171  	}
   172  
   173  	tests := []struct {
   174  		path []string
   175  		want string
   176  	}{
   177  		{
   178  			path: []string{},
   179  			want: `KIND:     CrdKind
   180  VERSION:  v1
   181  
   182  DESCRIPTION:
   183       <empty>
   184  `,
   185  		},
   186  	}
   187  
   188  	for _, test := range tests {
   189  		buf := bytes.Buffer{}
   190  		if err := PrintModelDescription(test.path, &buf, schema, gvk, false); err != nil {
   191  			t.Fatalf("Failed to PrintModelDescription for path %v: %v", test.path, err)
   192  		}
   193  		got := buf.String()
   194  		if got != test.want {
   195  			t.Errorf("Got:\n%v\nWant:\n%v\n", buf.String(), test.want)
   196  		}
   197  	}
   198  }
   199  

View as plain text