Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update OID #10

Merged
merged 4 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.22-alpine AS dev
FROM golang:1.23-alpine AS dev
RUN go install github.com/air-verse/air@latest
WORKDIR /app
COPY . /app/
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ docker run -d -p 8081:8081 --name go-snmp-olt-zte-c320 \
--network local-dev -e REDIS_HOST=redis-container \
-e REDIS_PORT=6379 -e REDIS_DB=0 \
-e REDIS_MIN_IDLE_CONNECTIONS=200 -e REDIS_POOL_SIZE=12000 \
-e REDIS_POOL_TIMEOUT=240 -e SNMP_HOST=192.168.213.174 \
-e SNMP_PORT=161 -e SNMP_COMMUNITY=homenetro \
-e REDIS_POOL_TIMEOUT=240 -e SNMP_HOST=x.x.x.x \
-e SNMP_PORT=161 -e SNMP_COMMUNITY=xxxx \
sumitroajiprabowo/go-snmp-olt-zte-c320:latest
```

Expand Down
4 changes: 2 additions & 2 deletions config/cfg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Board1Pon6:
onu_rx_power: ".500.20.2.2.2.1.10.285278470"
onu_tx_power: ".3.50.12.1.1.14.268502528"
onu_status_id : ".500.10.2.3.8.1.4.285278470"
onu_ip_address : ".3.50.16.1.1.10.268568064"
onu_ip_address : ".3.50.16.1.1.10.268502528"
onu_description : ".500.10.2.3.3.1.3.285278470"
onu_last_online_time: ".500.10.2.3.8.1.5.285278470"
onu_last_offline_time: ".500.10.2.3.8.1.6.285278470"
Expand All @@ -116,7 +116,7 @@ Board1Pon7:
onu_rx_power: ".500.20.2.2.2.1.10.285278471"
onu_tx_power: ".3.50.12.1.1.14.268502784"
onu_status_id : ".500.10.2.3.8.1.4.285278471"
onu_ip_address : ".3.50.16.1.1.10.268502528"
onu_ip_address : ".3.50.16.1.1.10.268502784"
onu_description : ".500.10.2.3.3.1.3.285278471"
onu_last_online_time: ".500.10.2.3.8.1.5.285278471"
onu_last_offline_time: ".500.10.2.3.8.1.6.285278471"
Expand Down
4 changes: 2 additions & 2 deletions config/config-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Board1Pon6:
onu_rx_power: ".500.20.2.2.2.1.10.285278470"
onu_tx_power: ".3.50.12.1.1.14.268502528"
onu_status_id : ".500.10.2.3.8.1.4.285278470"
onu_ip_address : ".3.50.16.1.1.10.268568064"
onu_ip_address : ".3.50.16.1.1.10.268502528"
onu_description : ".500.10.2.3.3.1.3.285278470"
onu_last_online_time: ".500.10.2.3.8.1.5.285278470"
onu_last_offline_time: ".500.10.2.3.8.1.6.285278470"
Expand All @@ -96,7 +96,7 @@ Board1Pon7:
onu_rx_power: ".500.20.2.2.2.1.10.285278471"
onu_tx_power: ".3.50.12.1.1.14.268502784"
onu_status_id : ".500.10.2.3.8.1.4.285278471"
onu_ip_address : ".3.50.16.1.1.10.268502528"
onu_ip_address : ".3.50.16.1.1.10.268502784"
onu_description : ".500.10.2.3.3.1.3.285278471"
onu_last_online_time: ".500.10.2.3.8.1.5.285278471"
onu_last_offline_time: ".500.10.2.3.8.1.6.285278471"
Expand Down
4 changes: 2 additions & 2 deletions config/config-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Board1Pon6:
onu_rx_power: ".500.20.2.2.2.1.10.285278470"
onu_tx_power: ".3.50.12.1.1.14.268502528"
onu_status_id: ".500.10.2.3.8.1.4.285278470"
onu_ip_address: ".3.50.16.1.1.10.268568064"
onu_ip_address: ".3.50.16.1.1.10.268502528"
onu_description: ".500.10.2.3.3.1.3.285278470"
onu_last_online_time: ".500.10.2.3.8.1.5.285278470"
onu_last_offline_time: ".500.10.2.3.8.1.6.285278470"
Expand All @@ -96,7 +96,7 @@ Board1Pon7:
onu_rx_power: ".500.20.2.2.2.1.10.285278471"
onu_tx_power: ".3.50.12.1.1.14.268502784"
onu_status_id: ".500.10.2.3.8.1.4.285278471"
onu_ip_address: ".3.50.16.1.1.10.268502528"
onu_ip_address: ".3.50.16.1.1.10.268502784"
onu_description: ".500.10.2.3.3.1.3.285278471"
onu_last_online_time: ".500.10.2.3.8.1.5.285278471"
onu_last_offline_time: ".500.10.2.3.8.1.6.285278471"
Expand Down
113 changes: 113 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package config

import (
"fmt"
"github.com/spf13/viper"
"os"
"path/filepath"
"testing"

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

func TestLoadConfig_Success(t *testing.T) {
// Create a temporary config file
tempDir := os.TempDir()
filePath := filepath.Join(tempDir, "test-config.yaml")

fmt.Println("Temporary config file path:", filePath)

file, err := os.Create(filePath)
if err != nil {
t.Fatalf("failed to create temp config file: %v", err)
}
defer func() {
file.Close()
os.Remove(file.Name())
}()

// Write sample config data to the file
configData := `
snmp_cfg:
ip: "127.0.0.1"
port: 161
community: "public"
redis_cfg:
host: "localhost"
port: "6379"
password: ""
db: 0
default_db: 0
min_idle_connections: 10
pool_size: 20
pool_timeout: 30
olt_cfg:
base_oid_1: "1.3.6.1.2.1"
base_oid_2: "1.3.6.1.2.2"
onu_id_name: "1.3.6.1.4.1"
onu_type: "1.3.6.1.4.2"
`
if _, err := file.Write([]byte(configData)); err != nil {
t.Fatalf("failed to write to temp config file: %v", err)
}
file.Close()

fmt.Println("File written successfully. Now setting Viper to use this file.")

// Point Viper to the temp file directly, bypassing LoadConfig()
viper.SetConfigFile(filePath)
viper.SetConfigType("yaml")

// Manually read the config file using Viper
if err := viper.ReadInConfig(); err != nil {
t.Fatalf("Viper failed to read the config: %v", err)
}

// Validate that the config is loaded correctly
assert.Equal(t, "127.0.0.1", viper.GetString("snmp_cfg.ip"), "expected IP to match")
assert.Equal(t, 161, viper.GetInt("snmp_cfg.port"), "expected Port to match")
}

func TestLoadConfig_FileNotFound(t *testing.T) {
// Attempt to load a non-existent config file
cfg, err := LoadConfig("non_existent_config.yaml")

// Assertions
assert.Nil(t, cfg)
assert.Error(t, err)
assert.Equal(t, "config file not found", err.Error())
}

func TestLoadConfig_InvalidConfig(t *testing.T) {
// Set up an invalid configuration file
invalidConfig := `
invalid_yaml_syntax:
- missing_colon "value"
`
configFile := "invalid_config.yaml"
err := os.WriteFile(configFile, []byte(invalidConfig), 0644)
assert.NoError(t, err)
defer os.Remove(configFile) // Clean up after the test

// Call the function under test
cfg, err := LoadConfig(configFile)

// Assertions
assert.Nil(t, cfg)
assert.Error(t, err)
}

func TestLoadConfig_FileExists(t *testing.T) {
// Cek apakah file konfigurasi ada
if _, err := os.Stat("cfg.yaml"); os.IsNotExist(err) {
t.Skip("File config.yaml tidak ditemukan, melewati tes.")
}

// Load the configuration
cfg, err := LoadConfig("config_test.go")

// Assertions
assert.Nil(t, cfg)
assert.Error(t, err)
assert.Equal(t, "config file not found", err.Error())
}
Loading