yunion.io/x/jsonutils@v1.0.0/yamlutils_test.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package jsonutils
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestYaml(t *testing.T) {
    22  	type SUser struct {
    23  		Name         string
    24  		Passwd       string
    25  		Keys         []string
    26  		LockPassword bool
    27  		FloatValue   float32
    28  	}
    29  	type SFile struct {
    30  		Path    string
    31  		Content string
    32  	}
    33  	type SCallback struct {
    34  		Url string
    35  	}
    36  	type Config struct {
    37  		Users       []SUser
    38  		WriteFiles  []SFile
    39  		Callback    *SCallback
    40  		Runcmd      []string
    41  		Bootcmd     []string
    42  		Packages    []string
    43  		DisableRoot int
    44  		SshPwauth   int
    45  	}
    46  	conf := Config{
    47  		Users: []SUser{
    48  			{
    49  				Name: "root",
    50  				Keys: []string{
    51  					"ssh-rsa AAAAA",
    52  				},
    53  			},
    54  			{
    55  				Name:         "yunion",
    56  				Passwd:       "123456",
    57  				LockPassword: true,
    58  			},
    59  		},
    60  		WriteFiles: []SFile{
    61  			{
    62  				Content: "#\n\n127.0.0.1\tlocalhost\n10.0.0.1\t212222\n\n",
    63  				Path:    "/etc/hosts",
    64  			},
    65  			{
    66  				Content: "gobuild",
    67  				Path:    "/etc/ansible/hosts",
    68  			},
    69  		},
    70  		Callback: &SCallback{
    71  			Url: "https://www.yunion.io/$INSTANCE_ID",
    72  		},
    73  		Runcmd: []string{
    74  			"mkdir -p /var/run/httpd",
    75  		},
    76  	}
    77  	jsonConf := Marshal(&conf)
    78  	yaml := jsonConf.YAMLString()
    79  
    80  	t.Logf("\n%s", yaml)
    81  
    82  	jsonConf2, err := ParseYAML(yaml)
    83  	if err != nil {
    84  		t.Errorf("%s", err)
    85  	} else {
    86  		yaml2 := jsonConf2.YAMLString()
    87  		t.Logf("\n%s", yaml2)
    88  
    89  		if yaml != yaml2 {
    90  			t.Errorf("yaml != yaml2")
    91  		}
    92  	}
    93  
    94  }