Skip to content

Commit 915f0ac

Browse files
committed
shorter comments
1 parent f18c901 commit 915f0ac

22 files changed

+398
-498
lines changed

wasp/alert.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ type AlertChecker struct {
2323
grafanaClient *grafana.Client
2424
}
2525

26-
// NewAlertChecker initializes a new AlertChecker instance by retrieving the Grafana URL and API token from the environment variables.
27-
// It panics if either the GRAFANA_URL or GRAFANA_TOKEN environment variable is not set, ensuring that the necessary configuration is available.
28-
// The function creates a Grafana client using the provided URL and API token, and sets up the AlertChecker with a default requirement label key and a logger.
29-
// It returns a pointer to the newly created AlertChecker instance, which can be used for checking alerts in Grafana.
26+
// NewAlertChecker initializes and returns a new AlertChecker instance.
27+
// It retrieves the Grafana URL and API token from environment variables
28+
// GRAFANA_URL and GRAFANA_TOKEN, respectively. If either is not set, it
29+
// panics. The function creates a Grafana client using these credentials
30+
// and sets up the AlertChecker with a logger and a requirement label key.
3031
func NewAlertChecker(t *testing.T) *AlertChecker {
3132
url := os.Getenv("GRAFANA_URL")
3233
if url == "" {
@@ -47,12 +48,9 @@ func NewAlertChecker(t *testing.T) *AlertChecker {
4748
}
4849
}
4950

50-
// AnyAlerts checks for alerts associated with a specific dashboard and requirement label.
51-
// It retrieves alert groups from the Grafana Alert Manager and scans through the alerts to determine
52-
// if any alert matches the provided dashboard UUID and requirement label value.
53-
// If an alert is found, it logs the alert details and marks that an alert has been raised.
54-
// The function returns the list of alert groups and any error encountered during the retrieval process.
55-
// If the test context is active and an alert was raised, it will mark the test as failed.
51+
// AnyAlerts checks for alerts in Grafana that match the specified dashboard UUID and requirement label value.
52+
// It returns a slice of AlertGroupsResponse and any error encountered during the retrieval of alert groups.
53+
// If an alert is found, it logs the alert details and marks the test as failed if a testing object is present.
5654
func (m *AlertChecker) AnyAlerts(dashboardUUID, requirementLabelValue string) ([]grafana.AlertGroupsResponse, error) {
5755
raised := false
5856
defer func() {
@@ -84,11 +82,9 @@ func (m *AlertChecker) AnyAlerts(dashboardUUID, requirementLabelValue string) ([
8482
return alertGroups, nil
8583
}
8684

87-
// CheckDashboardAlerts retrieves annotations of type "alert" from a specified Grafana dashboard within a given time range.
88-
// It returns a slice of annotations and an error if any occurred during the retrieval process.
89-
// If the retrieval is successful, the function sorts the annotations by time from oldest to newest and checks if any alerts are in an "alerting" state.
90-
// If at least one alert is found to be firing, it returns the annotations along with an error indicating that an alert was firing.
91-
// If no alerts are firing, it returns the annotations with a nil error.
85+
// CheckDashboardAlerts retrieves and checks alerts from a Grafana dashboard within a specified time range.
86+
// It returns a slice of annotations representing the alerts and an error if any alert is in the "alerting" state
87+
// or if there is an issue retrieving the annotations. The alerts are sorted by time from oldest to newest.
9288
func CheckDashboardAlerts(grafanaClient *grafana.Client, from, to time.Time, dashboardUID string) ([]grafana.Annotation, error) {
9389
annotationType := "alert"
9490
alerts, _, err := grafanaClient.GetAnnotations(grafana.AnnotationsQueryParams{

wasp/buffer.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@ type SliceBuffer[T any] struct {
77
Data []T
88
}
99

10-
// NewSliceBuffer initializes a new SliceBuffer with the specified capacity.
11-
// It returns a pointer to the newly created SliceBuffer, which contains an empty slice of the specified type.
12-
// This function is useful for creating a buffer that can hold elements of a generic type T, allowing for dynamic data storage.
10+
// NewSliceBuffer creates a new SliceBuffer with the specified capacity.
11+
// It initializes the buffer to hold elements of any type T, starting with an empty slice.
12+
// The function returns a pointer to the newly created SliceBuffer.
1313
func NewSliceBuffer[T any](cap int) *SliceBuffer[T] {
1414
return &SliceBuffer[T]{Capacity: cap, Data: make([]T, 0)}
1515
}
1616

17-
// Append adds a new element to the SliceBuffer. If the buffer has reached its capacity,
18-
// it will overwrite the oldest element. The function maintains the current index for
19-
// the next insertion, wrapping around when the end of the buffer is reached.
20-
// This allows for efficient storage of a fixed-size collection of elements.
17+
// Append adds an element of type T to the SliceBuffer. If the buffer's capacity is reached,
18+
// it overwrites the oldest element, maintaining a circular buffer. The index is incremented
19+
// after each append operation, wrapping around to the start if necessary.
2120
func (m *SliceBuffer[T]) Append(s T) {
2221
if m.Idx >= m.Capacity {
2322
m.Idx = 0

wasp/cluster.go

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,9 @@ type ClusterConfig struct {
6868
tmpHelmFilePath string
6969
}
7070

71-
// Defaults initializes default values for the Helm configuration and related paths in the ClusterConfig.
72-
// It sets the namespace, sync identifier, deployment timeout, resource requests and limits,
73-
// and ensures that default chart, Dockerfile, Docker ignore file, and build script are created
74-
// if their respective paths are not provided.
75-
// If any errors occur during file writing or path resolution, it returns the error encountered.
76-
// This function is typically called during the creation of a new ClusterProfile to ensure
77-
// that the configuration is complete and valid before proceeding with further operations.
71+
// Defaults sets default values for the ClusterConfig fields if they are not already set.
72+
// It initializes Helm values, file paths, and resource requests and limits with default values.
73+
// It returns an error if any file operations fail during the setup process.
7874
func (m *ClusterConfig) Defaults(a int) error {
7975
// TODO: will it be more clear if we move Helm values to a struct
8076
// TODO: or should it be like that for extensibility of a chart without reflection?
@@ -139,10 +135,8 @@ func (m *ClusterConfig) Defaults(a int) error {
139135
return nil
140136
}
141137

142-
// Validate checks the ClusterConfig for required fields.
143-
// It ensures that the Namespace is not empty and that the "jobs" key in HelmValues is set.
144-
// If any of these validations fail, it returns an error detailing the missing fields.
145-
// If all validations pass, it returns nil, indicating that the configuration is valid.
138+
// Validate checks the ClusterConfig for required fields and returns an error if any are missing.
139+
// Specifically, it ensures that the Namespace and HelmValues["jobs"] fields are not empty.
146140
func (m *ClusterConfig) Validate() (err error) {
147141
if m.Namespace == "" {
148142
err = errors.Join(err, ErrNoNamespace)
@@ -153,10 +147,8 @@ func (m *ClusterConfig) Validate() (err error) {
153147
return
154148
}
155149

156-
// parseECRImageURI parses an Amazon ECR image URI into its constituent parts: registry, repository, and tag.
157-
// It expects the URI to be in the format ${registry}/${repo}:${tag}.
158-
// If the format is invalid, it returns an error indicating the expected format.
159-
// On success, it returns the extracted registry, repository, and tag as strings, along with a nil error.
150+
// parseECRImageURI parses an Amazon ECR image URI into its constituent parts: registry, repository, and tag.
151+
// It returns an error if the URI does not match the expected format `${registry}/${repo}:${tag}`.
160152
func parseECRImageURI(uri string) (registry, repo, tag string, err error) {
161153
re := regexp.MustCompile(`^([^/]+)/([^:]+):(.+)$`)
162154
matches := re.FindStringSubmatch(uri)
@@ -174,11 +166,11 @@ type ClusterProfile struct {
174166
Cancel context.CancelFunc
175167
}
176168

177-
// NewClusterProfile creates a new ClusterProfile instance based on the provided ClusterConfig.
178-
// It validates the configuration and sets default values before initializing the ClusterProfile.
179-
// If the configuration includes an update for the image, it builds and pushes the image as part of the initialization process.
180-
// The function returns a pointer to the newly created ClusterProfile and an error if any issues occur during the process.
181-
// If the configuration is invalid or if there are errors in parsing the timeout duration, an appropriate error is returned.
169+
// NewClusterProfile creates a new ClusterProfile using the provided ClusterConfig.
170+
// It validates and applies default settings to the configuration. It logs the configuration
171+
// and sets a context with a timeout based on the test timeout duration specified in the config.
172+
// If UpdateImage is true, it builds and pushes the image. It returns the ClusterProfile and
173+
// any error encountered during the process.
182174
func NewClusterProfile(cfg *ClusterConfig) (*ClusterProfile, error) {
183175
if err := cfg.Validate(); err != nil {
184176
return nil, err
@@ -204,12 +196,7 @@ func NewClusterProfile(cfg *ClusterConfig) (*ClusterProfile, error) {
204196
return cp, nil
205197
}
206198

207-
// buildAndPushImage builds a Docker image using the specified configuration and pushes it to a container registry.
208-
// It constructs the command to execute based on the provided build script, Dockerfile, and context path,
209-
// along with the image tag, registry, and repository details parsed from the image URI.
210-
// If any errors occur during the parsing of the image URI or the execution of the build command,
211-
// the function returns the corresponding error.
212-
// On successful execution, it returns nil.
199+
// buildAndPushImage parses the ECR image URI from the configuration and constructs a command to build and push a Docker image. It returns an error if the URI parsing or command execution fails.
213200
func (m *ClusterProfile) buildAndPushImage() error {
214201
registry, repo, tag, err := parseECRImageURI(m.cfg.HelmValues["image"])
215202
if err != nil {
@@ -228,11 +215,10 @@ func (m *ClusterProfile) buildAndPushImage() error {
228215
return ExecCmd(cmd)
229216
}
230217

231-
// deployHelm installs a Helm chart using the specified test name and configuration settings.
232-
// It constructs a command to execute the Helm install operation, incorporating any Helm values
233-
// provided in the configuration. The function also ensures that the temporary Helm file is
234-
// removed after execution. If the command execution fails, it returns an error indicating
235-
// the failure of the deployment process.
218+
// deployHelm installs a Helm chart using the specified testName as the release name.
219+
// It constructs the Helm command with the chart path, values, namespace, and timeout
220+
// from the ClusterProfile configuration. It returns any error encountered during the
221+
// execution of the Helm command.
236222
func (m *ClusterProfile) deployHelm(testName string) error {
237223
//nolint
238224
defer os.Remove(m.cfg.tmpHelmFilePath)
@@ -247,12 +233,10 @@ func (m *ClusterProfile) deployHelm(testName string) error {
247233
return ExecCmd(cmd.String())
248234
}
249235

250-
// Run executes the deployment of a Helm chart and tracks the associated jobs.
251-
// It generates a unique test name, ensuring it starts with a letter, and then
252-
// calls the deployHelm method to initiate the deployment. If the deployment
253-
// is successful, it retrieves the number of jobs from the configuration and
254-
// invokes the TrackJobs method to monitor the job status.
255-
// The function returns an error if any step in the process fails.
236+
// Run generates a unique test name, modifies it to comply with Helm naming rules,
237+
// and deploys a Helm chart using this name. It retrieves the number of jobs from
238+
// the Helm values and tracks these jobs within the specified namespace. It returns
239+
// an error if any step in the process fails.
256240
func (m *ClusterProfile) Run() error {
257241
testName := uuid.NewString()[0:8]
258242
tn := []rune(testName)

wasp/cmd.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,18 @@ import (
99
"github.com/rs/zerolog/log"
1010
)
1111

12-
// ExecCmd executes a command in the shell and logs its output.
13-
// It takes a command string as input and returns an error if the command fails to execute or if there are issues with the output streams.
14-
// The command's standard output and standard error are processed by a logging function that captures and logs the output in real-time.
15-
// If the command completes successfully, ExecCmd returns nil; otherwise, it returns an error detailing the failure.
12+
// ExecCmd executes the given command string in a shell environment.
13+
// It logs the command's output using the default logging mechanism.
14+
// It returns any error encountered during the execution of the command.
1615
func ExecCmd(command string) error {
1716
return ExecCmdWithStreamFunc(command, func(m string) {
1817
log.Info().Str("Text", m).Msg("Command output")
1918
})
2019
}
2120

22-
// readStdPipe reads lines from the provided io.ReadCloser pipe and passes each line to the specified streamFunc.
23-
// If streamFunc is nil, the lines will be ignored. The function continues reading until the pipe is closed or an error occurs.
24-
// It is typically used to handle output from command execution in a streaming manner.
21+
// readStdPipe reads lines from the provided io.ReadCloser pipe and passes each line to the streamFunc callback.
22+
// If streamFunc is nil, the lines are not processed. This function is typically used to handle output streams
23+
// from executed commands, allowing real-time processing of command output.
2524
func readStdPipe(pipe io.ReadCloser, streamFunc func(string)) {
2625
scanner := bufio.NewScanner(pipe)
2726
scanner.Split(bufio.ScanLines)
@@ -33,11 +32,9 @@ func readStdPipe(pipe io.ReadCloser, streamFunc func(string)) {
3332
}
3433
}
3534

36-
// ExecCmdWithStreamFunc executes a command specified by the command string and streams its output
37-
// to the provided outputFunction. The outputFunction is called with each line of output from both
38-
// standard output and standard error streams. The function returns an error if there is an issue
39-
// starting the command, creating pipes, or waiting for the command to complete.
40-
// If the command executes successfully, it will return nil.
35+
// ExecCmdWithStreamFunc executes a shell command and streams its output to the provided outputFunction.
36+
// The command's standard output and standard error are captured and passed to outputFunction line by line.
37+
// It returns any error encountered during the execution of the command.
4138
func ExecCmdWithStreamFunc(command string, outputFunction func(string)) error {
4239
c := strings.Split(command, " ")
4340
cmd := exec.Command(c[0], c[1:]...)

wasp/dashboard/cmd/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"github.com/smartcontractkit/chainlink-testing-framework/wasp/dashboard"
55
)
66

7+
// main initializes a default dashboard without non-functional requirements (NFRs) or extensions.
8+
// It creates a new dashboard instance and deploys it, using environment variables for configuration.
9+
// If an error occurs during dashboard creation or deployment, the function panics.
710
func main() {
811
// just default dashboard, no NFRs, no dashboard extensions
912
// see examples/alerts.go for an example extension

0 commit comments

Comments
 (0)