From e288ae6ea9a692365dbeeb422b602805c477dd8b Mon Sep 17 00:00:00 2001
From: Manuel Kieweg <manuel.kieweg@h-da.de>
Date: Fri, 18 Jun 2021 19:32:21 +0100
Subject: [PATCH] lightweight Dockerfile.alpine

---
 Dockerfile.alpine                 |   1 -
 api/apiIntegration_test.go        | 102 +++++++-----------------------
 nucleus/device.go                 |  12 ++++
 nucleus/principalNetworkDomain.go |  13 ++--
 4 files changed, 38 insertions(+), 90 deletions(-)

diff --git a/Dockerfile.alpine b/Dockerfile.alpine
index acfb373d1..e09e1d572 100644
--- a/Dockerfile.alpine
+++ b/Dockerfile.alpine
@@ -6,7 +6,6 @@ WORKDIR /src/gosdn
 COPY . .
 RUN apk add --no-cache git
 RUN git config --global url."https://$GITLAB_USER:$GITLAB_TOKEN@code.fbi.h-da.de".insteadOf "https://code.fbi.h-da.de"
-RUN go mod tidy
 RUN GOOS=linux go build -o gosdn ./cmd/gosdn
 
 FROM alpine
diff --git a/api/apiIntegration_test.go b/api/apiIntegration_test.go
index 2b4de2dc5..ac9b29625 100644
--- a/api/apiIntegration_test.go
+++ b/api/apiIntegration_test.go
@@ -1,87 +1,15 @@
 package api
 
 import (
-	"os"
 	"testing"
 
 	"code.fbi.h-da.de/cocsn/api/go/gosdn/pnd"
 	tpb "code.fbi.h-da.de/cocsn/api/go/gosdn/transport"
-	"code.fbi.h-da.de/cocsn/gosdn/nucleus/util/proto"
+	"github.com/google/uuid"
 	guuid "github.com/google/uuid"
-	gpb "github.com/openconfig/gnmi/proto/gnmi"
-	log "github.com/sirupsen/logrus"
 	"github.com/spf13/viper"
-	pb "google.golang.org/protobuf/proto"
 )
 
-const unreachable = "203.0.113.10:6030"
-const testPath = "/system/config/hostname"
-
-var testAddress = "141.100.70.170:6030"
-var testAPIEndpoint = "gosdn-latest.apps.ocp.fbi.h-da.de"
-var testUsername = "admin"
-var testPassword = "arista"
-var opt *tpb.TransportOption
-var gnmiMessages map[string]pb.Message
-
-func TestMain(m *testing.M) {
-	testSetupIntegration()
-	os.Exit(m.Run())
-}
-
-func testSetupIntegration() {
-	if os.Getenv("GOSDN_LOG") == "nolog" {
-		log.SetLevel(log.PanicLevel)
-	}
-
-	addr := os.Getenv("GOSDN_TEST_ENDPOINT")
-	if addr != "" {
-		testAddress = addr
-		log.Infof("GOSDN_TEST_ENDPOINT set to %v", testAddress)
-	}
-	api := os.Getenv("GOSDN_TEST_API_ENDPOINT")
-	if api != "" {
-		testAPIEndpoint = api
-		log.Infof("GOSDN_TEST_API_ENDPOINT set to %v", testAPIEndpoint)
-	}
-	u := os.Getenv("GOSDN_TEST_USER")
-	if u != "" {
-		testUsername = u
-		log.Infof("GOSDN_TEST_USER set to %v", testUsername)
-	}
-	p := os.Getenv("GOSDN_TEST_PASSWORD")
-	if p != "" {
-		testPassword = p
-		log.Infof("GOSDN_TEST_PASSWORD set to %v", testPassword)
-	}
-
-	gnmiMessages = map[string]pb.Message{
-		"../test/proto/cap-resp-arista-ceos":                  &gpb.CapabilityResponse{},
-		"../test/proto/req-full-node":                         &gpb.GetRequest{},
-		"../test/proto/req-full-node-arista-ceos":             &gpb.GetRequest{},
-		"../test/proto/req-interfaces-arista-ceos":            &gpb.GetRequest{},
-		"../test/proto/req-interfaces-interface-arista-ceos":  &gpb.GetRequest{},
-		"../test/proto/req-interfaces-wildcard":               &gpb.GetRequest{},
-		"../test/proto/resp-full-node":                        &gpb.GetResponse{},
-		"../test/proto/resp-full-node-arista-ceos":            &gpb.GetResponse{},
-		"../test/proto/resp-interfaces-arista-ceos":           &gpb.GetResponse{},
-		"../test/proto/resp-interfaces-interface-arista-ceos": &gpb.GetResponse{},
-		"../test/proto/resp-interfaces-wildcard":              &gpb.GetResponse{},
-		"../test/proto/resp-set-system-config-hostname":       &gpb.SetResponse{},
-	}
-	for k, v := range gnmiMessages {
-		if err := proto.Read(k, v); err != nil {
-			log.Fatalf("error parsing %v: %v", k, err)
-		}
-	}
-
-	opt = &tpb.TransportOption{
-		Address:  testAddress,
-		Username: testUsername,
-		Password: testPassword,
-	}
-}
-
 func TestApiIntegration(t *testing.T) {
 	// TDOO: Remove once openshift grpc support is available
 	t.Skip("skipped due to openshift limitations")
@@ -111,24 +39,38 @@ func TestApiIntegration(t *testing.T) {
 			cliPnd := viper.GetString("CLI_PND")
 			cliSbi := viper.GetString("CLI_SBI")
 
+			suid, err := uuid.Parse(cliSbi)
+			if err != nil {
+				t.Error(err)
+			}
+			puid, err := uuid.Parse(cliPnd)
+			if err != nil {
+				t.Error(err)
+			}
+
+			opt := &tpb.TransportOption{
+				Address:  testAddress,
+				Username: testUsername,
+				Password: testPassword,
+				TransportOption: &tpb.TransportOption_GnmiTransportOption{
+					GnmiTransportOption: &tpb.GnmiTransportOption{},
+				},
+			}
 			if _, err := addDevice(
 				testAPIEndpoint,
-				testUsername,
-				testPassword,
-				cliSbi,
-				cliPnd,
-				testAddress,
 				"test-device",
+				opt,
+				suid,
+				puid,
 			); (err != nil) != tt.wantErr {
 				t.Errorf("gosdn cli add-device error = %v, wantErr %v", err, tt.wantErr)
 				return
 			}
 			did := viper.GetString("LAST_DEVICE_UUID")
 
-			_, err := getDevice(
+			_, err = getDevice(
 				testAPIEndpoint,
 				cliPnd,
-				testPath,
 				did,
 			)
 			if (err != nil) != tt.wantErr {
diff --git a/nucleus/device.go b/nucleus/device.go
index 4c828772f..a324d1813 100644
--- a/nucleus/device.go
+++ b/nucleus/device.go
@@ -24,6 +24,18 @@ func NewDevice(name string, opt *tpb.TransportOption, sbi southbound.SouthboundI
 		name = namesgenerator.GetRandomName(0)
 	}
 
+	if opt.Csbi {
+		return &CsbiDevice{
+			CommonDevice: CommonDevice{
+				UUID:      uuid.New(),
+				GoStruct:  sbi.Schema().Root,
+				sbi:       sbi,
+				transport: t,
+				name:      name,
+			},
+		}, nil
+	}
+
 	return &CommonDevice{
 		UUID:      uuid.New(),
 		GoStruct:  sbi.Schema().Root,
diff --git a/nucleus/principalNetworkDomain.go b/nucleus/principalNetworkDomain.go
index 1b91f5721..d2b4d6d3d 100644
--- a/nucleus/principalNetworkDomain.go
+++ b/nucleus/principalNetworkDomain.go
@@ -412,19 +412,14 @@ func (pnd *pndImplementation) createCsbiDevice(name string, d *cpb.Deployment, o
 			panic(err)
 		}
 		opt.Address = deviceDetails.Address
-		t, err := NewTransport(opt, csbi)
+
+		d, err := NewDevice(name, opt, csbi)
 		if err != nil {
 			panic(err)
 		}
-		d := &CsbiDevice{
-			CommonDevice: CommonDevice{
-				name:      name,
-				UUID:      id,
-				transport: t,
-			},
-		}
+		d.(*CsbiDevice).UUID = id
 		ch <- DeviceDetails{TransportOption: opt}
-		if err := pnd.devices.Add(d, d.name); err != nil {
+		if err := pnd.devices.Add(d, d.Name()); err != nil {
 			panic(err)
 		}
 		pnd.callback(id, nil)
-- 
GitLab