Skip to content

Commit

Permalink
chore: fix all lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
leoparente committed Jan 31, 2025
1 parent 816610b commit adcd484
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 83 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ permissions:
pull-requests: write

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '1.23.x'
check-latest: true
- name: Lint
uses: golangci/golangci-lint-action@971e284b6050e8a5849b72094c50ab08da042db8 #v6.1.1
with:
version: v1.62
working-directory: .
args: --config ../.github/golangci.yaml

test:
runs-on: ubuntu-latest
Expand Down
46 changes: 24 additions & 22 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,30 @@ import (
"go.uber.org/zap/zapcore"
)

const routineKey config.ContextKey = "routine"

var (
Debug bool
SelfTelemetry bool
ServerHost string
ServerPort uint64
Set []string
FeatureGates string
debug bool
selfTelemetry bool
serverHost string
serverPort uint64
set []string
featureGates string
)

func Run(cmd *cobra.Command, args []string) {
func run(_ *cobra.Command, _ []string) {
config := config.Config{
Debug: Debug,
SelfTelemetry: SelfTelemetry,
ServerHost: ServerHost,
ServerPort: ServerPort,
Set: Set,
FeatureGates: FeatureGates,
Debug: debug,
SelfTelemetry: selfTelemetry,
ServerHost: serverHost,
ServerPort: serverPort,
Set: set,
FeatureGates: featureGates,
}
// logger
var logger *zap.Logger
atomicLevel := zap.NewAtomicLevel()
if Debug {
if debug {
atomicLevel.SetLevel(zap.DebugLevel)
} else {
atomicLevel.SetLevel(zap.InfoLevel)
Expand All @@ -60,7 +62,7 @@ func Run(cmd *cobra.Command, args []string) {

// handle signals
done := make(chan bool, 1)
rootCtx, cancelFunc := context.WithCancel(context.WithValue(context.Background(), "routine", "mainRoutine"))
rootCtx, cancelFunc := context.WithCancel(context.WithValue(context.Background(), routineKey, "mainRoutine"))

go func() {
sigs := make(chan os.Signal, 1)
Expand Down Expand Up @@ -98,15 +100,15 @@ func main() {
Use: "run",
Short: "Run opentelemetry-infinity",
Long: `Run opentelemetry-infinity`,
Run: Run,
Run: run,
}

runCmd.PersistentFlags().BoolVarP(&Debug, "debug", "d", false, "Enable verbose (debug level) output")
runCmd.PersistentFlags().BoolVarP(&SelfTelemetry, "self_telemetry", "s", false, "Enable self telemetry for collectors. It is disabled by default to avoid port conflict")
runCmd.PersistentFlags().StringVarP(&ServerHost, "server_host", "a", "localhost", "Define REST Host")
runCmd.PersistentFlags().Uint64VarP(&ServerPort, "server_port", "p", 10222, "Define REST Port")
runCmd.PersistentFlags().StringSliceVarP(&Set, "set", "e", nil, "Define opentelemetry set")
runCmd.PersistentFlags().StringVarP(&FeatureGates, "feature_gates", "f", "", "Define opentelemetry feature gates")
runCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "Enable verbose (debug level) output")
runCmd.PersistentFlags().BoolVarP(&selfTelemetry, "self_telemetry", "s", false, "Enable self telemetry for collectors. It is disabled by default to avoid port conflict")
runCmd.PersistentFlags().StringVarP(&serverHost, "server_host", "a", "localhost", "Define REST Host")
runCmd.PersistentFlags().Uint64VarP(&serverPort, "server_port", "p", 10222, "Define REST Port")
runCmd.PersistentFlags().StringSliceVarP(&set, "set", "e", nil, "Define opentelemetry set")
runCmd.PersistentFlags().StringVarP(&featureGates, "feature_gates", "f", "", "Define opentelemetry feature gates")

rootCmd.AddCommand(runCmd)
if err := rootCmd.Execute(); err != nil {
Expand Down
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package config

import "time"

// ContextKey represents the key for the context
type ContextKey string

// Status represents the status of the service
type Status struct {
StartTime time.Time `json:"start_time"`
Expand Down
17 changes: 14 additions & 3 deletions otlpinf/otlpinf.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ import (
"gopkg.in/yaml.v3"
)

const routineKey config.ContextKey = "routine"

// RunnerInfo represents the runner info
type RunnerInfo struct {
Policy config.Policy
Instance runner.Runner
}

// OltpInf represents the otlpinf routine
type OltpInf struct {
logger *zap.Logger
conf *config.Config
Expand All @@ -29,13 +33,15 @@ type OltpInf struct {
capabilities []byte
}

// New creates a new otlpinf routine
func New(logger *zap.Logger, c *config.Config) (OltpInf, error) {
return OltpInf{logger: logger, conf: c, policies: make(map[string]RunnerInfo)}, nil
}

// Start starts the otlpinf routine
func (o *OltpInf) Start(ctx context.Context, cancelFunc context.CancelFunc) error {
o.stat.StartTime = time.Now()
o.ctx = context.WithValue(ctx, "routine", "otlpInfRoutine")
o.ctx = context.WithValue(ctx, routineKey, "otlpInfRoutine")
o.cancelFunction = cancelFunc

var err error
Expand Down Expand Up @@ -63,8 +69,13 @@ func (o *OltpInf) Start(ctx context.Context, cancelFunc context.CancelFunc) erro
return nil
}

// Stop stops the otlpinf routine
func (o *OltpInf) Stop(ctx context.Context) {
o.logger.Info("routine call for stop otlpinf", zap.Any("routine", ctx.Value("routine")))
defer os.RemoveAll(o.policiesDir)
o.cancelFunction()
defer func() {
if err := os.RemoveAll(o.policiesDir); err != nil {
o.logger.Error("error removing policies directory", zap.Error(err))
}
}()
defer o.cancelFunction()
}
52 changes: 32 additions & 20 deletions otlpinf/otlpinf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ import (
)

const (
TestHost = "localhost"
PoliciesApi = "/api/v1/policies"
HttpYamlContent = "application/x-yaml"
ErrorMessage = "HTTP status code = %v, wanted %v"
NewErrorMessage = "New() error = %v"
PostErrorMessage = "http.Post() error = %v"
YamlErrorMessage = "yaml.NewEncoder() error = %v"
TestHost = "localhost"
PoliciesAPI = "/api/v1/policies"
HTTPYamlContent = "application/x-yaml"
)

func TestOtlpInfRestApis(t *testing.T) {
Expand Down Expand Up @@ -55,7 +51,7 @@ func TestOtlpInfRestApis(t *testing.T) {

// Act and Assert
w = httptest.NewRecorder()
req, _ = http.NewRequest("GET", PoliciesApi, nil)
req, _ = http.NewRequest("GET", PoliciesAPI, nil)
otlp.router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)

Expand All @@ -73,14 +69,14 @@ func TestOtlpInfRestApis(t *testing.T) {

// Act and Assert invalid header
w = httptest.NewRecorder()
req, _ = http.NewRequest("POST", PoliciesApi, nil)
req, _ = http.NewRequest("POST", PoliciesAPI, nil)
otlp.router.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)

// Act and Assert invalid policy config
w = httptest.NewRecorder()
req, _ = http.NewRequest("POST", PoliciesApi, bytes.NewBuffer([]byte("invalid\n")))
req.Header.Set("Content-Type", HttpYamlContent)
req, _ = http.NewRequest("POST", PoliciesAPI, bytes.NewBuffer([]byte("invalid\n")))
req.Header.Set("Content-Type", HTTPYamlContent)
otlp.router.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
Expand All @@ -98,7 +94,7 @@ func TestOtlpinfCreateDeletePolicy(t *testing.T) {

// Act and Assert
otlp, err := New(logger, &cfg)
assert.NoError(t, err, NewErrorMessage, err)
assert.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
err = otlp.Start(ctx, cancel)
Expand Down Expand Up @@ -136,32 +132,39 @@ func TestOtlpinfCreateDeletePolicy(t *testing.T) {
err = yaml.NewEncoder(&buf).Encode(data)
assert.NoError(t, err)

resp, err := http.Post(server+PoliciesApi, HttpYamlContent, &buf)
resp, err := http.Post(server+PoliciesAPI, HTTPYamlContent, &buf)
assert.NoError(t, err)
err = resp.Body.Close()
assert.NoError(t, err)

// Assert
assert.Equal(t, http.StatusCreated, resp.StatusCode, resp)

// Act and Assert Get Policies
resp, err = http.Get(server + PoliciesApi)
resp, err = http.Get(server + PoliciesAPI)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
err = resp.Body.Close()
assert.NoError(t, err)

// Act and Assert Get Valid Policy
resp, err = http.Get(server + "/api/v1/policies/" + policyName)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
err = resp.Body.Close()
assert.NoError(t, err)

// Act Try to insert same policy
err = yaml.NewEncoder(&buf).Encode(data)
assert.NoError(t, err)

resp, err = http.Post(server+PoliciesApi, HttpYamlContent, &buf)
resp, err = http.Post(server+PoliciesAPI, HTTPYamlContent, &buf)
assert.NoError(t, err)

// Assert
assert.Equal(t, http.StatusConflict, resp.StatusCode)

err = resp.Body.Close()
assert.NoError(t, err)
// Act Delete Policy
req, err := http.NewRequest("DELETE", server+"/api/v1/policies/"+policyName, nil)
assert.NoError(t, err)
Expand All @@ -172,6 +175,8 @@ func TestOtlpinfCreateDeletePolicy(t *testing.T) {

// Assert
assert.Equal(t, http.StatusOK, resp.StatusCode)
err = resp.Body.Close()
assert.NoError(t, err)

otlp.Stop(ctx)
}
Expand Down Expand Up @@ -209,11 +214,13 @@ func TestOtlpinfCreateInvalidPolicy(t *testing.T) {
err = yaml.NewEncoder(&buf).Encode(data)
assert.NoError(t, err)

resp, err := http.Post(server+PoliciesApi, HttpYamlContent, &buf)
resp, err := http.Post(server+PoliciesAPI, HTTPYamlContent, &buf)
assert.NoError(t, err)

// Assert
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
err = resp.Body.Close()
assert.NoError(t, err)

// Act try to insert policy with invalid config
data[policyName] = map[string]interface{}{
Expand All @@ -224,11 +231,13 @@ func TestOtlpinfCreateInvalidPolicy(t *testing.T) {
err = yaml.NewEncoder(&buf).Encode(data)
assert.NoError(t, err)

resp, err = http.Post(server+PoliciesApi, HttpYamlContent, &buf)
resp, err = http.Post(server+PoliciesAPI, HTTPYamlContent, &buf)
assert.NoError(t, err)

// Assert
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
err = resp.Body.Close()
assert.NoError(t, err)

// Act try to insert two policies at once
data[policyName] = map[string]interface{}{
Expand All @@ -244,11 +253,13 @@ func TestOtlpinfCreateInvalidPolicy(t *testing.T) {
err = yaml.NewEncoder(&buf).Encode(data)
assert.NoError(t, err)

resp, err = http.Post(server+PoliciesApi, HttpYamlContent, &buf)
resp, err = http.Post(server+PoliciesAPI, HTTPYamlContent, &buf)
assert.NoError(t, err)

// Assert
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
err = resp.Body.Close()
assert.NoError(t, err)

otlp.Stop(ctx)
}
Expand Down Expand Up @@ -279,5 +290,6 @@ func TestOtlpinfStartError(t *testing.T) {
}

// Reset the temporary directory environment variable to its original value
os.Unsetenv("TMPDIR")
err = os.Unsetenv("TMPDIR")
assert.NoError(t, err)
}
Loading

0 comments on commit adcd484

Please sign in to comment.