github.com/taubyte/tau-cli@v0.1.13-0.20240326000942-487f0d57edfc/tests/helpers_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path"
     7  	"path/filepath"
     8  	"reflect"
     9  	"strings"
    10  
    11  	commonTest "github.com/taubyte/tau-cli/common/test"
    12  	"github.com/taubyte/tau-cli/singletons/session"
    13  	"github.com/taubyte/utils/x509"
    14  )
    15  
    16  func basicGetConfigString(profileName string, projectName string) func(dir string) []byte {
    17  	return func(dir string) []byte {
    18  		return []byte(`
    19  profiles:
    20    ` + profileName + `:
    21      provider: github
    22      token: 123456
    23      default: true
    24      git_username: taubyte-test
    25      git_email: taubytetest@gmail.com
    26      type: Remote
    27      network: sandbox.taubyte.com
    28  projects:
    29    ` + projectName + `:
    30      defaultprofile: ` + profileName + `
    31      location: ` + projectName)
    32  	}
    33  }
    34  
    35  func basicValidConfigString(profileName string, projectName string) func(dir string) []byte {
    36  	return func(dir string) []byte {
    37  		return []byte(`
    38  profiles:
    39    ` + profileName + `:
    40      provider: github
    41      token: ` + commonTest.GitToken() + `
    42      default: true
    43      git_username: ` + commonTest.GitUser + `
    44      git_email: taubytetest@gmail.com
    45      type: Remote
    46      network: sandbox.taubyte.com
    47  projects:
    48    ` + projectName + `:
    49      defaultprofile: ` + profileName + `
    50      location: ` + projectName)
    51  	}
    52  }
    53  
    54  func certWriteFilesInDir(hostName string, pathArgs ...string) func(dir string) {
    55  	var _path string
    56  	if len(pathArgs) == 0 {
    57  		_path = ""
    58  	} else {
    59  		// TODO make this magical
    60  		_path = path.Join(pathArgs...)
    61  	}
    62  	return func(dir string) {
    63  		if len(hostName) == 0 {
    64  			return
    65  		}
    66  		certData, privateKey, err := x509.GenerateCert(hostName)
    67  		if err != nil {
    68  			panic(fmt.Sprintf("GenerateCert for host `%s` failed with: %s", hostName, err))
    69  		}
    70  
    71  		// Write cert file
    72  		certFilePath, err := filepath.Abs(path.Join(dir, _path, "testcert.crt"))
    73  		if err != nil {
    74  			panic(fmt.Sprintf("Make path: %s failed with error: %s", path.Join(_path, "testcert.crt"), err.Error()))
    75  		}
    76  		err = os.WriteFile(certFilePath, certData, 0640)
    77  		if err != nil {
    78  			panic(fmt.Sprintf("Write file: %s failed with error: %s", certFilePath, err.Error()))
    79  		}
    80  
    81  		// Write key file
    82  		keyFilePath, err := filepath.Abs(path.Join(dir, _path, "key.key"))
    83  		if err != nil {
    84  			panic(fmt.Sprintf("Make path: %s failed with error: %s", path.Join(_path, "key.key"), err.Error()))
    85  		}
    86  
    87  		err = os.WriteFile(keyFilePath, privateKey, 0640)
    88  		if err != nil {
    89  			panic(fmt.Sprintf("Write file: %s failed with error: %s", keyFilePath, err.Error()))
    90  		}
    91  	}
    92  }
    93  
    94  // TODO Move to utils
    95  func stringContainsAll(query string, items []string) bool {
    96  	for _, s := range items {
    97  		if !strings.Contains(query, s) {
    98  			return false
    99  		}
   100  	}
   101  	return true
   102  }
   103  
   104  // TODO Move to utils
   105  func stringContainsAny(query string, items []string) bool {
   106  	for _, s := range items {
   107  		if strings.Contains(query, s) {
   108  			return true
   109  		}
   110  	}
   111  	return false
   112  }
   113  
   114  // evaluateSession helper
   115  func expectProfileName(expected string) func(g session.Getter) error {
   116  	return func(g session.Getter) error {
   117  		profileName, _ := g.ProfileName()
   118  		if profileName != expected {
   119  			return fmt.Errorf("expected profile name to be `%s`, got `%s`", expected, profileName)
   120  		}
   121  
   122  		return nil
   123  	}
   124  }
   125  
   126  // evaluateSession helper
   127  func expectSelectedProject(expected string) func(g session.Getter) error {
   128  	return func(g session.Getter) error {
   129  		selectedProject, _ := g.SelectedProject()
   130  		if selectedProject != expected {
   131  			return fmt.Errorf("expected project name to be `%s`, got `%s`", expected, selectedProject)
   132  		}
   133  
   134  		return nil
   135  	}
   136  }
   137  
   138  // evaluateSession helper
   139  func expectedSelectedNetwork(expected string) func(g session.Getter) error {
   140  	return func(g session.Getter) error {
   141  		network, _ := g.SelectedNetwork()
   142  		if network != expected {
   143  			return fmt.Errorf("Network does not match, %s != %s", expected, network)
   144  		}
   145  
   146  		return nil
   147  	}
   148  }
   149  
   150  // evaluateSession helper
   151  func expectedSelectedCustomNetwork(expectedNetwork, expectedFQDN string) func(g session.Getter) error {
   152  	return func(g session.Getter) error {
   153  		fqdn, _ := g.CustomNetworkUrl()
   154  		if expectedFQDN != fqdn {
   155  			return fmt.Errorf("FQDN does not match, %s != %s", expectedFQDN, fqdn)
   156  		}
   157  
   158  		network, _ := g.SelectedNetwork()
   159  		if expectedNetwork != network {
   160  			return fmt.Errorf("Network does not match, %s != %s", expectedNetwork, network)
   161  		}
   162  
   163  		return nil
   164  	}
   165  }
   166  
   167  func isEmpty(val interface{}) bool {
   168  	switch v := val.(type) {
   169  	case string:
   170  		return v == ""
   171  	case int, int16, int32, int64, int8:
   172  		return v == 0
   173  	case uint, uint16, uint32, uint64, uint8:
   174  		return v == 0
   175  	case float32, float64:
   176  		return v == 0.0
   177  	case bool:
   178  		return !v
   179  	default:
   180  		reflectVal := reflect.ValueOf(val)
   181  		switch reflectVal.Kind() {
   182  		case reflect.Array, reflect.Map, reflect.Slice:
   183  			return reflectVal.Len() == 0
   184  		case reflect.Chan, reflect.Func, reflect.Interface, reflect.Ptr:
   185  			return reflectVal.IsNil()
   186  		case reflect.Struct:
   187  			return reflectVal.IsZero()
   188  		default:
   189  			return false
   190  		}
   191  	}
   192  }
   193  
   194  func ConfirmEmpty(values ...any) error {
   195  	for _, val := range values {
   196  		if !isEmpty(val) {
   197  			return fmt.Errorf("%T (%#v) is not empty", val, val)
   198  		}
   199  	}
   200  
   201  	return nil
   202  }