Repository: clouditor/clouditor
Pull request #262 (2021-06-23 06:31:20)

Fixed embedded metric test
More testing, accidentally merged in #254 too early.

codecov[bot]: # [Codecov](https://codecov.io/gh/clouditor/clouditor/pull/262?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor) Report
> Merging [#262](https://codecov.io/gh/clouditor/clouditor/pull/262?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor) (f8a0b13) into [main](https://codecov.io/gh/clouditor/clouditor/commit/263e8b85e7f6fa30353305ad9b474b44fecdb5fb?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor) (263e8b8) will **not change** coverage.
> The diff coverage is `66.66%`.

[![Impacted file tree graph](https://codecov.io/gh/clouditor/clouditor/pull/262/graphs/tree.svg?width=650&height=150&src=pr&token=syrXsw5LTX&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor)](https://codecov.io/gh/clouditor/clouditor/pull/262?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor)

```diff
@@           Coverage Diff           @@
##             main     #262   +/-   ##
=======================================
  Coverage   56.25%   56.25%           
=======================================
  Files          35       35           
  Lines        1472     1472           
=======================================
  Hits          828      828           
- Misses        557      558    +1     
+ Partials       87       86    -1     
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `56.25% <66.66%> (ø)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/clouditor/clouditor/pull/262?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor) | Coverage Δ | |
|---|---|---|
| [service/orchestrator/orchestrator.go](https://codecov.io/gh/clouditor/clouditor/pull/262/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor#diff-c2VydmljZS9vcmNoZXN0cmF0b3Ivb3JjaGVzdHJhdG9yLmdv) | `72.72% <66.66%> (+9.09%)` | :arrow_up: |
| [service/assessment/assessment.go](https://codecov.io/gh/clouditor/clouditor/pull/262/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor#diff-c2VydmljZS9hc3Nlc3NtZW50L2Fzc2Vzc21lbnQuZ28=) | `80.00% <0.00%> (-13.34%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/clouditor/clouditor/pull/262?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/clouditor/clouditor/pull/262?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor). Last update [263e8b8...f8a0b13](https://codecov.io/gh/clouditor/clouditor/pull/262?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor).


Patch:
diff --git a/clouditor.code-workspace b/clouditor.code-workspace
index 3374e176..31dc0478 100644
--- a/clouditor.code-workspace
+++ b/clouditor.code-workspace
@@ -6,6 +6,7 @@
 	],
 	"settings": {
 		"cSpell.words": [
+			"Kubernetes",
 			"clouditor",
 			"grpc"
 		],
diff --git a/service/orchestrator/orchestrator.go b/service/orchestrator/orchestrator.go
index d12972ae..a8e178e5 100644
--- a/service/orchestrator/orchestrator.go
+++ b/service/orchestrator/orchestrator.go
@@ -47,6 +47,8 @@ var metrics []*assessment.Metric
 var metricIndex map[int32]*assessment.Metric
 var log *logrus.Entry
 
+var DefaultMetricsFile = "metrics.json"
+
 // Service is an implementation of the Clouditor Orchestrator service
 type Service struct {
 	orchestrator.UnimplementedOrchestratorServer
@@ -55,7 +57,7 @@ type Service struct {
 func init() {
 	log = logrus.WithField("component", "orchestrator")
 
-	if err := loadMetrics(); err != nil {
+	if err := LoadMetrics(DefaultMetricsFile); err != nil {
 		log.Errorf("Could not load embedded metrics. Will continue with empty metric list: %v", err)
 	}
 
@@ -65,14 +67,14 @@ func init() {
 	}
 }
 
-func loadMetrics() (err error) {
+func LoadMetrics(metricsFile string) (err error) {
 	var (
 		b []byte
 	)
 
-	b, err = f.ReadFile("metrics.json")
+	b, err = f.ReadFile(metricsFile)
 	if err != nil {
-		return fmt.Errorf("error while loading metrics.json: %w", err)
+		return fmt.Errorf("error while loading %s: %w", metricsFile, err)
 	}
 
 	err = json.Unmarshal(b, &metrics)


Test patch:
diff --git a/service/orchestrator/orchestrator_test.go b/service/orchestrator/orchestrator_test.go
index 92cf218f..f42690c4 100644
--- a/service/orchestrator/orchestrator_test.go
+++ b/service/orchestrator/orchestrator_test.go
@@ -27,6 +27,7 @@ package orchestrator_test
 
 import (
 	"context"
+	"io/fs"
 	"testing"
 
 	"clouditor.io/clouditor/api/assessment"
@@ -66,3 +67,13 @@ func TestGetMetric(t *testing.T) {
 	assert.NotNil(t, metric)
 	assert.Equal(t, request.MetricId, metric.Id)
 }
+
+func TestLoad(t *testing.T) {
+	var err = service_orchestrator.LoadMetrics("notfound.json")
+
+	assert.ErrorIs(t, err, fs.ErrNotExist)
+
+	err = service_orchestrator.LoadMetrics("metrics.json")
+
+	assert.Nil(t, err)
+}