Skip to content

Commit

Permalink
add test files and .golangci.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
don2112e committed Sep 12, 2024
1 parent 7d054ef commit 5b7303b
Show file tree
Hide file tree
Showing 5 changed files with 631 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ jobs:
run: go build -v -mod=mod -o target/hello-world-app ./src

- name: Test
run: go test -v ./src
run: go test -mod=mod -v ./src
101 changes: 101 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
---
run:
# default concurrency is a available CPU number
concurrency: 4

# timeout for analysis, e.g. 30s, 5m, default is 1m
deadline: 2m

# exit code when at least one issue was found, default is 1
issues-exit-code: 1

# include test files or not, default is true
tests: true

# list of build tags, all linters use it. Default is empty list.
build-tags:
- adp-playground
# which dirs to skip: they won't be analyzed;
# can use regexp here: generated.*, regexp is applied on full path;
# default value is empty list, but next dirs are always skipped independently
# from this option's value:
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
# Dummy stuff, fill if we want to skip
skip-dirs:
- src/external_libs
- autogenerated_by_my_lib
- static_modules
- zip-validation

# which files to skip: they will be analyzed, but issues from them
# won't be reported. Default value is empty list, but there is
# no need to include all autogenerated files, we confidently recognize
# autogenerated files. If it's not please let us know.
skip-files:
- ".*\\.my\\.go$"
- lib/bad.go


# by default isn't set. If set we pass it to "go list -mod={option}". From "go help modules":
# If invoked with -mod=readonly, the go command is disallowed from the implicit
# automatic updating of go.mod described above. Instead, it fails when any changes
# to go.mod are needed. This setting is most useful to check that go.mod does
# not need updates, such as in a continuous integration and testing system.
# If invoked with -mod=vendor, the go command assumes that the vendor
# directory holds the correct copies of dependencies and ignores
# the dependency descriptions in go.mod.
#modules-download-mode: readonly|release|vendor
modules-download-mode:


# output configuration options
output:
# colored-line-number|line-number|json|tab|checkstyle, default is "colored-line-number"
format: line-number

# print lines of code with issue, default is true
print-issued-lines: true

# print linter name in the end of issue text, default is true
print-linter-name: true

issues:
# List of regexps of issue texts to exclude, empty list by default.
# But independently from this option we use default exclude patterns,
# it can be disabled by `exclude-use-default: false`. To list all
# excluded by default patterns execute `golangci-lint run --help`
exclude:

# Independently from option `exclude` we use default exclude patterns,
# it can be disabled by this option. To list all
# excluded by default patterns execute `golangci-lint run --help`.
# Default value for this option is true.
exclude-use-default: false

# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
max-per-linter: 0

# Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
max-same-issues: 0

# Show only new issues: if there are unstaged changes or untracked files,
# only those changes are analyzed, else only changes in HEAD~ are analyzed.
# It's a super-useful option for integration of golangci-lint into existing
# large codebase. It's not practical to fix all existing issues at the moment
# of integration: much better don't allow issues in new code.
# Default is false.
new: false

# Show only new issues created after git revision `REV`
# new-from-rev: HEAD~1
new-from-rev:

# Show only new issues created in git patch with set file path.
new-from-patch:

# golangci.com configuration
# https://github.com/golangci/golangci/wiki/Configuration
service:
golangci-lint-version: 1.43.0 # use fixed version to not introduce new linters unexpectedly
prepare:
- echo "here I can run custom commands, but no preparation needed"
208 changes: 208 additions & 0 deletions src/internal/configuration/configuration_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
// COPYRIGHT Ericsson 2024

// The copyright to the computer program(s) herein is the property of
// Ericsson Inc. The programs may be used and/or copied only with written
// permission from Ericsson Inc. or in accordance with the terms and
// conditions stipulated in the agreement/contract under which the
// program(s) have been supplied.

package configuration

import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"math/big"
"os"
"strconv"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

const (
key string = "OS_ENV"
defaultValueInt int = 1
defaultValueString string = "string"
)

func TestGetConfig(t *testing.T) {
t.Parallel()

testConfig := AppConfig

assert.NotNil(t, testConfig,
"Instance should not be nil")

assert.Equal(t, 8050, testConfig.LocalPort,
"Port should be 8050, but got : "+strconv.Itoa(testConfig.LocalPort))

assert.Equal(t, "http", testConfig.LocalProtocol,
"Protocol should be http, but got : "+testConfig.LocalProtocol)

assert.Equal(t, "certificate.pem", testConfig.CertFile,
"Certificate file name should be `certificate.pem`, but got : "+testConfig.CertFile)

assert.Equal(t, "key.pem", testConfig.KeyFile,
"Keyfile file name should be `key.pem`, but got : "+testConfig.KeyFile)

assert.Equal(t, "", testConfig.LogControlFile,
"LogControlFile should be an empty string, but got : "+testConfig.LogControlFile)
}

func TestReloadAppConfig(t *testing.T) {
t.Parallel()
config1 := AppConfig
ReloadAppConfig()
config2 := AppConfig
assert.NotSame(t, config1, config2,
"AppConfig should be different pointer after ReloadAppConfig()")
}

func TestGetOsEnvIntSet(t *testing.T) {
t.Setenv(key, "123")

result := getOsEnvInt(key, defaultValueInt)
assert.Equal(t, 123, result)
}

func TestGetOsEnvIntUnset(t *testing.T) {
t.Parallel()

result := getOsEnvInt(key, defaultValueInt)
assert.Equal(t, defaultValueInt, result)
}

func TestGetOsEnvIntSetBadInt(t *testing.T) {
t.Setenv(key, "abc")

result := getOsEnvInt(key, defaultValueInt)
assert.Equal(t, defaultValueInt, result)
}

func TestGetOsEnvStringSet(t *testing.T) {
t.Setenv(key, "someValue")

result := getOsEnvString(key, defaultValueString)
assert.Equal(t, "someValue", result)
}

func TestGetOsEnvStringUnset(t *testing.T) {
t.Parallel()

result := getOsEnvString(key, defaultValueString)
assert.Equal(t, defaultValueString, result)
}

func TestInvalidTLSConfigs(t *testing.T) {
tlsConfig := NewTLSConfig()
assert.Nil(t, tlsConfig)

logMtlsConfig := LogmTLSConfig()
assert.Nil(t, logMtlsConfig)
}

func TestTLSConfigs(t *testing.T) {
err := generateCACert()
assert.Nil(t, err, fmt.Sprintf("error generating cacert: %v", err))
err = generateKeyCertPair()
assert.Nil(t, err, fmt.Sprintf("error generating certs: %v", err))
t.Setenv("CA_CERT_FILE_NAME", "cacert.crt")
t.Setenv("APP_CERT", "cert.pem")
t.Setenv("APP_KEY", "key.pem")
ReloadAppConfig()

tlsConfig := NewTLSConfig()
assert.NotNil(t, tlsConfig)

logMtlsConfig := LogmTLSConfig()
assert.NotNil(t, logMtlsConfig)

t.Cleanup(func() {
e := os.Remove("cacert.crt")
assert.Nil(t, e, fmt.Sprintf("error deleting cacert.crt: %v", e))
e = os.Remove("cert.pem")
assert.Nil(t, e, fmt.Sprintf("error deleting cert.pem: %v", e))
e = os.Remove("key.pem")
assert.Nil(t, e, fmt.Sprintf("error deleting key.pem: %v", e))
})
}

func generateCACert() error {
file, err := os.OpenFile("cacert.crt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o666)
if err != nil {
return err
}
err = file.Close()

return err
}

func generateKeyCertPair() error {
privateKey, err := rsa.GenerateKey(rand.Reader, 1024)
if err != nil {
return err
}

serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 64)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return err
}

keyUsage := x509.KeyUsageDigitalSignature
keyUsage |= x509.KeyUsageKeyEncipherment

template := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{"test"},
},
NotBefore: time.Now(),
NotAfter: time.Now().Add(10000),

KeyUsage: keyUsage,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
}

derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &privateKey.PublicKey, privateKey)
if err != nil {
return err
}

certOut, err := os.Create("cert.pem")
if err != nil {
return err
}

if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
return err
}

if err := certOut.Close(); err != nil {
return err
}

keyOut, err := os.OpenFile("key.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
if err != nil {
return err
}

privBytes, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
return err
}

if err := pem.Encode(keyOut, &pem.Block{Type: "PRIVATE KEY", Bytes: privBytes}); err != nil {
return err
}

err = keyOut.Close()

return err
}
Loading

0 comments on commit 5b7303b

Please sign in to comment.