github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/integration_test/itest/config.go (about)

     1  package itest
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/telepresenceio/telepresence/v2/pkg/client"
     9  	"github.com/telepresenceio/telepresence/v2/pkg/filelocation"
    10  )
    11  
    12  // SetConfig creates a config from the configYml provided and assigns it to a new context which
    13  // is returned. Use this if you are testing components of the config.yml, otherwise you can use setDefaultConfig.
    14  func SetConfig(ctx context.Context, configDir, configYml string) (context.Context, error) {
    15  	config, err := os.Create(filepath.Join(configDir, "config.yml"))
    16  	if err != nil {
    17  		return ctx, err
    18  	}
    19  
    20  	_, err = config.WriteString(configYml)
    21  	if err != nil {
    22  		return ctx, err
    23  	}
    24  	config.Close()
    25  
    26  	// Load env if it isn't loaded already
    27  	ctx = filelocation.WithAppUserConfigDir(ctx, configDir)
    28  	if env := client.GetEnv(ctx); env == nil {
    29  		env, err = client.LoadEnv()
    30  		if err != nil {
    31  			return ctx, err
    32  		}
    33  		ctx = client.WithEnv(ctx, env)
    34  	}
    35  
    36  	cfg, err := client.LoadConfig(ctx)
    37  	if err != nil {
    38  		return ctx, err
    39  	}
    40  	ctx = client.WithConfig(ctx, cfg)
    41  	return ctx, nil
    42  }