github.com/midokura/kubeedge@v1.2.0-mido.0/tests/e2e/utils/config.go (about)

     1  /*
     2  Copyright 2019 The KubeEdge 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  package utils
    17  
    18  import (
    19  	"encoding/json"
    20  	"math/rand"
    21  	"os"
    22  	"path/filepath"
    23  	"time"
    24  )
    25  
    26  const letterBytes = "abcdefghijklmnopqrstuvwxyz0123456789"
    27  
    28  type vmSpec struct {
    29  	Ip       string `json:"ip"`
    30  	Username string `json:"username"`
    31  	Passwd   string `json:"password"`
    32  }
    33  
    34  //config.json decode struct
    35  type Config struct {
    36  	AppImageUrl                    []string          `json:"image_url"`
    37  	K8SMasterForKubeEdge           string            `json:"k8smasterforkubeedge"`
    38  	Nodes                          map[string]vmSpec `json:"k8snodes"`
    39  	NumOfNodes                     int               `json:"node_num"`
    40  	ImageRepo                      string            `json:"imagerepo"`
    41  	K8SMasterForProvisionEdgeNodes string            `json:"k8smasterforprovisionedgenodes"`
    42  	CloudImageUrl                  string            `json:"cloudimageurl"`
    43  	EdgeImageUrl                   string            `json:"edgeimageurl"`
    44  	Namespace                      string            `json:"namespace"`
    45  	ControllerStubPort             int               `json:"controllerstubport"`
    46  	Protocol                       string            `json:"protocol"`
    47  	DockerHubUserName              string            `json:"dockerhubusername"`
    48  	DockerHubPassword              string            `json:"dockerhubpassword"`
    49  	MqttEndpoint                   string            `json:"mqttendpoint"`
    50  	KubeConfigPath                 string            `json:"kubeconfigpath"`
    51  }
    52  
    53  //config struct
    54  var config *Config
    55  
    56  //get config.json path
    57  func LoadConfig() Config {
    58  	if config == nil {
    59  		config = loadConfigJsonFromPath()
    60  	}
    61  	return *config
    62  }
    63  
    64  //loadConfigJsonFromPath reads the test configuration and builds a Config object.
    65  func loadConfigJsonFromPath() *Config {
    66  	path := getConfigPath()
    67  	_, err := filepath.Abs(filepath.Dir(path))
    68  	if err != nil {
    69  		Infof("Failed to get Abs path: %v", err)
    70  		panic(err)
    71  	}
    72  	var config *Config = &Config{}
    73  	configFile, err := os.Open(path)
    74  	if err != nil {
    75  		panic(err)
    76  	}
    77  	decoder := json.NewDecoder(configFile)
    78  	err = decoder.Decode(config)
    79  	if err != nil {
    80  		panic(err)
    81  	}
    82  	return config
    83  }
    84  
    85  //getConfigPath returns the configuration path provided in the env var name. In case the env var is not
    86  //set, the default configuration path is returned
    87  func getConfigPath() string {
    88  	path := os.Getenv("TESTCONFIG")
    89  	if path == "" {
    90  		path = "config.json"
    91  	}
    92  	return path
    93  }
    94  
    95  func RandomInt(min, max int) int {
    96  	return min + rand.Intn(max-min)
    97  }
    98  
    99  //function to Generate Random string
   100  func GetRandomString(length int) string {
   101  	str := "0123456789abcdefghijklmnopqrstuvwxyz"
   102  	bytes := []byte(str)
   103  	result := []byte{}
   104  	r := rand.New(rand.NewSource(time.Now().UnixNano()))
   105  	for i := 0; i < length; i++ {
   106  		result = append(result, bytes[r.Intn(len(bytes))])
   107  	}
   108  	return string(result)
   109  }