...

Source file src/github.com/redhat-developer/odo/tests/helper/helper_telemetry.go

Documentation: github.com/redhat-developer/odo/tests/helper

     1  package helper
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"os"
     7  
     8  	_ "github.com/onsi/ginkgo/v2"
     9  	. "github.com/onsi/gomega"
    10  
    11  	"github.com/redhat-developer/odo/pkg/config"
    12  	envcontext "github.com/redhat-developer/odo/pkg/config/context"
    13  	"github.com/redhat-developer/odo/pkg/preference"
    14  	"github.com/redhat-developer/odo/pkg/segment"
    15  )
    16  
    17  const (
    18  	DebugTelemetryFileEnv = "ODO_DEBUG_TELEMETRY_FILE"
    19  )
    20  
    21  func setDebugTelemetryFile(value string) error {
    22  	return os.Setenv(DebugTelemetryFileEnv, value)
    23  }
    24  
    25  // EnableTelemetryDebug creates a temp file to use for debugging telemetry.
    26  // it also sets up envs and cfg for the same
    27  func EnableTelemetryDebug() preference.Client {
    28  	Expect(os.Setenv(segment.TrackingConsentEnv, "yes")).NotTo(HaveOccurred())
    29  
    30  	ctx := context.Background()
    31  	envConfig, err := config.GetConfiguration()
    32  	Expect(err).To(BeNil())
    33  	ctx = envcontext.WithEnvConfig(ctx, *envConfig)
    34  
    35  	cfg, _ := preference.NewClient(ctx)
    36  	err = cfg.SetConfiguration(preference.ConsentTelemetrySetting, "true")
    37  	Expect(err).To(BeNil())
    38  	tempFile, err := os.CreateTemp("", "telemetry")
    39  	Expect(err).NotTo(HaveOccurred())
    40  	Expect(setDebugTelemetryFile(tempFile.Name())).NotTo(HaveOccurred())
    41  	Expect(tempFile.Close()).NotTo(HaveOccurred())
    42  
    43  	return cfg
    44  }
    45  
    46  func GetDebugTelemetryFile() string {
    47  	return os.Getenv(DebugTelemetryFileEnv)
    48  }
    49  
    50  func ClearTelemetryFile() {
    51  	err := os.Truncate(GetDebugTelemetryFile(), 0)
    52  	Expect(err).ShouldNot(HaveOccurred())
    53  }
    54  
    55  // GetTelemetryDebugData gets telemetry data dumped into temp file for testing/debugging
    56  func GetTelemetryDebugData() segment.TelemetryData {
    57  	var data []byte
    58  	var td segment.TelemetryData
    59  	telemetryFile := GetDebugTelemetryFile()
    60  	Eventually(func() string {
    61  		d, err := os.ReadFile(telemetryFile)
    62  		Expect(err).To(BeNil())
    63  		return string(d)
    64  	}, 10, 1).Should(ContainSubstring("event"))
    65  	data, err := os.ReadFile(telemetryFile)
    66  	Expect(err).NotTo(HaveOccurred())
    67  	Expect(json.Unmarshal(data, &td)).NotTo(HaveOccurred())
    68  	return td
    69  }
    70  
    71  // ResetTelemetry resets the telemetry back to original values
    72  func ResetTelemetry() {
    73  	Expect(os.Setenv(segment.TrackingConsentEnv, "no")).NotTo(HaveOccurred())
    74  	Expect(os.Unsetenv(DebugTelemetryFileEnv))
    75  
    76  	ctx := context.Background()
    77  	envConfig, err := config.GetConfiguration()
    78  	Expect(err).To(BeNil())
    79  	ctx = envcontext.WithEnvConfig(ctx, *envConfig)
    80  
    81  	cfg, _ := preference.NewClient(ctx)
    82  	err = cfg.SetConfiguration(preference.ConsentTelemetrySetting, "true")
    83  	Expect(err).NotTo(HaveOccurred())
    84  	Expect(segment.IsTelemetryEnabled(cfg, *envConfig)).To(BeFalse())
    85  }
    86  

View as plain text