Skip to content

Commit

Permalink
Merge pull request #5 from brian-intel/unitTests
Browse files Browse the repository at this point in the history
feat: Unit tests
  • Loading branch information
brian-intel authored Jan 11, 2024
2 parents 393a51d + 183ba78 commit 9b8c7e4
Show file tree
Hide file tree
Showing 29 changed files with 1,101 additions and 279 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ docs/sitemap.xml.gz
docs/*
profile-launcher/profile-launcher
smoke_tests_output.log
profile-launcher/*.out
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Core Services


```bash
go run main.go -e TEST_ENV=aaa,NEW=abc --configdir ./test-profile/valid-profile --inputsrc /dev/video4 --target_device CPU
```

## Render mode

```bash
xhost +local:docker
```
6 changes: 0 additions & 6 deletions profile-launcher/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ RUN apk update && \
apk add make autoconf libtool protobuf-dev && \
rm -rf /var/lib/apt/lists/*
WORKDIR /app
RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.31.0
RUN go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.3.0
# Compile API
RUN wget https://raw.githubusercontent.com/openvinotoolkit/model_server/main/src/kfserving_api/grpc_predict_v2.proto
RUN echo 'option go_package = "./grpc-client";' >> grpc_predict_v2.proto
RUN protoc --go_out="./" --go-grpc_out="./" ./grpc_predict_v2.proto

COPY . .
RUN go mod tidy && make build-binary
Expand Down
21 changes: 0 additions & 21 deletions profile-launcher/Dockerfile.unit-test

This file was deleted.

12 changes: 4 additions & 8 deletions profile-launcher/Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
# Copyright © 2023 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

.PHONY: build build-test unit-test build-binary
.PHONY: build unit-test build-binary

build:
docker build --target bin --output=. .

build-test:
cd test-profile && docker build -t test:dev .

unit-test:
@docker build -f Dockerfile.unit-test -t go-unit-test:dev .
@docker run --rm -v .:/test -v /var/run/docker.sock:/var/run/docker.sock go-unit-test:dev

build-binary:
@echo "building executeable profile-launcher..."
@go build -o profile-launcher
@echo "done"

test:
go test -p 1 -cover ./...
161 changes: 161 additions & 0 deletions profile-launcher/functions/containers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// ----------------------------------------------------------------------------------
// Copyright 2024 Intel Corp.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ----------------------------------------------------------------------------------

package functions

import (
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/docker/docker/api/types/mount"
"gopkg.in/yaml.v3"
)

func GetYamlConfig(configDir string) (Containers, error) {
profileConfigPath := filepath.Join(configDir, "profile_config.yaml")
contents, err := os.ReadFile(profileConfigPath)
if err != nil {
return Containers{}, fmt.Errorf("Unable to read config file: %v, error: %v",
configDir, err)
}

containersArray := Containers{}
err = yaml.Unmarshal(contents, &containersArray)
if err != nil {
return Containers{}, fmt.Errorf("error: %v", err)
}

return containersArray, nil
}

func (containerArray *Containers) GetEnv(configDir string) error {
for i, cont := range containerArray.Containers {
profileConfigPath := filepath.Join(configDir, cont.EnvironmentVariableFiles)
contents, err := os.ReadFile(profileConfigPath)
if err != nil {
err = fmt.Errorf("Unable to read config file: %v, error: %v",
configDir, err)
return err
}
containerArray.Containers[i].Envs = strings.Split(string(contents[:]), "\n")
// Set Target Device ENV
if containerArray.TargetDevice != "" {
containerArray.Containers[i].Envs = append(containerArray.Containers[i].Envs, containerArray.TargetDevice)
}
}
return nil
}

func (containerArray *Containers) OverrideEnv(envOverrides []string) error {
for contIndex, cont := range containerArray.Containers {
for _, override := range envOverrides {
notFound := true
if override != "" {
overrideArray := strings.Split(override, "=")
if len(overrideArray) != 2 {
return fmt.Errorf("env format incorrect, ensure env is EnvName=Value.")
}
for envIndex, env := range cont.Envs {
if strings.Contains(env, overrideArray[0]+"=") {
containerArray.Containers[contIndex].Envs[envIndex] = override
notFound = false
}
}
if notFound {
containerArray.Containers[contIndex].Envs = append(containerArray.Containers[contIndex].Envs, override)
}
}
}
}
return nil
}

// Setup volume mounts for the countainer
func (containerArray *Containers) SetVolumes(volumes []string) error {
// First create the mounts from the volume inputs
var volumeMountParam []mount.Mount
for _, vol := range volumes {
tmpVol, err := CreateVolumeMount(vol)
if err != nil {
return fmt.Errorf("Failed to create volume mount")
}
volumeMountParam = append(volumeMountParam, tmpVol)
}

// Second get the volumes found in the config yaml
for contIndex, cont := range containerArray.Containers {
for _, vol := range cont.Volumes {
tmpVol, err := CreateVolumeMount(vol)
if err != nil {
return fmt.Errorf("Failed to create volume mount")
}
containerArray.Containers[contIndex].HostConfig.Mounts = append(containerArray.Containers[contIndex].HostConfig.Mounts, tmpVol)
}
// Append any input volumes to the config yaml volume list
containerArray.Containers[contIndex].HostConfig.Mounts = append(containerArray.Containers[contIndex].HostConfig.Mounts, volumeMountParam...)
}

return nil
}

// Setup device mounts and ENV based on the targe device input
func (containerArray *Containers) SetTargetDevice() error {
if containerArray.TargetDevice == "" {
containerArray.SetPrivileged()
} else if containerArray.TargetDevice == "CPU" {
// CPU do nothing
return nil
} else if containerArray.TargetDevice == "GPU" ||
strings.Contains(containerArray.TargetDevice, "MULTI") ||
containerArray.TargetDevice == "AUTO" {
// GPU set to privileged so we can access all GPU
containerArray.SetPrivileged()
} else if strings.Contains(containerArray.TargetDevice, "GPU.") {
// GPU.X set access to a specific GPU device and set the correct ENV
TargetDeviceArray := strings.Split(containerArray.TargetDevice, ".")
DeviceNum, errNum := strconv.Atoi(TargetDeviceArray[1])
if errNum != nil {
return errNum
}
TargetDeviceNum := 128 + DeviceNum
TargetGpu := strconv.Itoa(TargetDeviceNum)
// Set GPU Device
containerArray.SetHostDevice("/dev/dri/renderD" + TargetGpu)
} else {
return fmt.Errorf("Target device not supported")
}
return nil
}

// Setup devices and other mounts based on the inputsrc
func (containerArray *Containers) SetInputSrc() error {
if containerArray.InputSrc == "" {
return errors.New("InputSrc was not set. Exiting profile launcher.")
} else if strings.Contains(containerArray.InputSrc, "/video") {
containerArray.SetHostDevice(containerArray.InputSrc)
}

for contIndex, _ := range containerArray.Containers {
containerArray.Containers[contIndex].Envs = append(containerArray.Containers[contIndex].Envs, "INPUTSRC="+containerArray.InputSrc)
}

return nil
}
Loading

0 comments on commit 9b8c7e4

Please sign in to comment.