diff --git a/getdeck/sources/inline.py b/getdeck/sources/inline.py index 1eb9ca7..fd6f257 100644 --- a/getdeck/sources/inline.py +++ b/getdeck/sources/inline.py @@ -2,7 +2,7 @@ from getdeck.fetch.types import DeckfileAux, SourceAux -from getdeck.sources.generator import RenderBehavior +from getdeck.sources.generator import RenderBehavior, RenderError from getdeck.sources.types import K8sSourceFile logger = logging.getLogger("deck") @@ -11,12 +11,18 @@ class Inline(RenderBehavior): def render(self, deckfile_aux: DeckfileAux, source_aux: SourceAux, **kwargs): try: + # optional arguments + arguments = {} + if self.namespace: + arguments["namespace"] = self.namespace + source_file = K8sSourceFile( name="Deckfile", content=source_aux.source.content, - namespace=self.namespace, + **arguments, ) return [source_file] except Exception as e: - logger.error(f"Error processing file: {e}") - raise e + logger.error(f"Processing inline source failed: {deckfile_aux.location}") + logger.debug(e) + raise RenderError(str(e)) diff --git a/test/deckfile/deck.helm.yaml b/test/deckfile/deck.dashboard.yaml similarity index 100% rename from test/deckfile/deck.helm.yaml rename to test/deckfile/deck.dashboard.yaml diff --git a/test/sources/deck.empty.yaml b/test/sources/deck.empty.yaml new file mode 100644 index 0000000..ce9ea58 --- /dev/null +++ b/test/sources/deck.empty.yaml @@ -0,0 +1,40 @@ +version: "1" + +cluster: + provider: k3d + minVersion: 4.0.0 + name: test-empty-file + nativeConfig: + apiVersion: k3d.io/v1alpha4 + kind: Simple + servers: 1 + agents: 1 + image: rancher/k3s:v1.22.9-k3s1 + options: + k3s: + extraArgs: + - arg: --disable=traefik + nodeFilters: + - server:* + ports: + - port: 61346:80 + nodeFilters: + - loadbalancer + - port: 8443:443 + nodeFilters: + - loadbalancer + - port: 31820:31820/UDP + nodeFilters: + - agent:0 + +decks: + - name: empty + namespace: default + sources: + - type: inline + content: + { + "kind": "Namespace", + "apiVersion": "v1", + "metadata": { "name": "content", "labels": { "name": "content" } }, + } diff --git a/test/sources/deck.file.yaml b/test/sources/deck.file.yaml index 787be86..411ccb9 100644 --- a/test/sources/deck.file.yaml +++ b/test/sources/deck.file.yaml @@ -3,7 +3,7 @@ version: "1" cluster: provider: k3d minVersion: 4.0.0 - name: test-sources-file + name: test-file-sources nativeConfig: apiVersion: k3d.io/v1alpha4 kind: Simple @@ -28,22 +28,9 @@ cluster: - agent:0 decks: - - name: hello + - name: test namespace: default sources: - - type: helm - ref: https://kubernetes.github.io/ingress-nginx - chart: ingress-nginx - releaseName: ingress-nginx - namespace: ingress-nginx - helmArgs: - - --create-namespace - parameters: - - name: controller.admissionWebhooks.enabled - value: false - - name: controller.ingressClassResource.default - value: true - - type: file ref: ./hello.yaml @@ -60,11 +47,3 @@ decks: ref: git@github.com:Getdeck/getdeck.git targetRevision: main path: test/beiboot/hello.yaml - - - type: inline - content: - { - "kind": "Namespace", - "apiVersion": "v1", - "metadata": { "name": "content", "labels": { "name": "content" } }, - } diff --git a/test/sources/deck.helm.yaml b/test/sources/deck.helm.yaml new file mode 100644 index 0000000..85bf974 --- /dev/null +++ b/test/sources/deck.helm.yaml @@ -0,0 +1,45 @@ +version: "1" + +cluster: + provider: k3d + minVersion: 4.0.0 + name: test-helm-sources + nativeConfig: + apiVersion: k3d.io/v1alpha4 + kind: Simple + servers: 1 + agents: 1 + image: rancher/k3s:v1.22.9-k3s1 + options: + k3s: + extraArgs: + - arg: --disable=traefik + nodeFilters: + - server:* + ports: + - port: 61346:80 + nodeFilters: + - loadbalancer + - port: 8443:443 + nodeFilters: + - loadbalancer + - port: 31820:31820/UDP + nodeFilters: + - agent:0 + +decks: + - name: test + namespace: default + sources: + - type: helm + ref: https://kubernetes.github.io/ingress-nginx + chart: ingress-nginx + releaseName: ingress-nginx + namespace: ingress-nginx + helmArgs: + - --create-namespace + parameters: + - name: controller.admissionWebhooks.enabled + value: false + - name: controller.ingressClassResource.default + value: true diff --git a/test/sources/deck.inline.yaml b/test/sources/deck.inline.yaml new file mode 100644 index 0000000..619b064 --- /dev/null +++ b/test/sources/deck.inline.yaml @@ -0,0 +1,49 @@ +version: "1" + +cluster: + provider: k3d + minVersion: 4.0.0 + name: test-inline-sources + nativeConfig: + apiVersion: k3d.io/v1alpha4 + kind: Simple + servers: 1 + agents: 1 + image: rancher/k3s:v1.22.9-k3s1 + options: + k3s: + extraArgs: + - arg: --disable=traefik + nodeFilters: + - server:* + ports: + - port: 61346:80 + nodeFilters: + - loadbalancer + - port: 8443:443 + nodeFilters: + - loadbalancer + - port: 31820:31820/UDP + nodeFilters: + - agent:0 + +decks: + - name: test + namespace: default + sources: + - type: inline + content: + { + "kind": "Namespace", + "apiVersion": "v1", + "metadata": { "name": "content", "labels": { "name": "content" } }, + } + + - type: inline + content: + kind: Namespace + apiVersion: v1 + metadata: + - name: content + - labels: + - name: content diff --git a/test/src/fetch/test_fetch.py b/test/src/fetch/test_fetch.py index dff1de1..4e33f6b 100644 --- a/test/src/fetch/test_fetch.py +++ b/test/src/fetch/test_fetch.py @@ -4,13 +4,34 @@ class FetchDataTest(TestCase): - def test_local(self): + def test_local_empty(self): location = "./test/deckfile/deck.empty.yaml" data_aux = fetch_data(location) self.assertIsNotNone(data_aux.deckfile) self.assertIsNotNone(data_aux.deckfile_aux) self.assertEqual(len(data_aux.source_auxs), 1) + def test_local_inline(self): + location = "./test/sources/deck.inline.yaml" + data_aux = fetch_data(location) + self.assertIsNotNone(data_aux.deckfile) + self.assertIsNotNone(data_aux.deckfile_aux) + self.assertEqual(len(data_aux.source_auxs), 2) + + def test_local_file(self): + location = "./test/sources/deck.file.yaml" + data_aux = fetch_data(location) + self.assertIsNotNone(data_aux.deckfile) + self.assertIsNotNone(data_aux.deckfile_aux) + self.assertEqual(len(data_aux.source_auxs), 5) + + def test_local_helm(self): + location = "./test/sources/deck.helm.yaml" + data_aux = fetch_data(location) + self.assertIsNotNone(data_aux.deckfile) + self.assertIsNotNone(data_aux.deckfile_aux) + self.assertEqual(len(data_aux.source_auxs), 1) + def test_git_with_no_deckfile(self): location = "git@github.com:Getdeck/getdeck.git" with self.assertRaises(RuntimeError): diff --git a/test/src/fetch/test_source_fetcher.py b/test/src/fetch/test_source_fetcher.py index 7ef6df0..9e75383 100644 --- a/test/src/fetch/test_source_fetcher.py +++ b/test/src/fetch/test_source_fetcher.py @@ -65,7 +65,7 @@ def test_none_helm_source(self): class SourceFetcherTest(TestCase): - def test_inline(self): + def test_file(self): location = "https://raw.githubusercontent.com/Getdeck/getdeck/main/test/sources/resources/hello.yaml" source = DeckfileFileSource(ref=location) source_aux = SourceAux(location=location) diff --git a/test/src/sources/test_render_behavior.py b/test/src/sources/test_render_behavior.py new file mode 100644 index 0000000..0e15dca --- /dev/null +++ b/test/src/sources/test_render_behavior.py @@ -0,0 +1,34 @@ +from unittest import TestCase +from getdeck.deckfile.file import DeckfileInlineSource +from getdeck.fetch.types import DeckfileAux, SourceAux +from getdeck.sources.inline import Inline +import itertools + + +class InlineTest(TestCase): + def test_render(self): + test_namespaces = [None, "default", "test"] + test_contents = [{}] + combinations = itertools.product(test_namespaces, test_contents) + + for combination in combinations: + namespace = combination[0] + content = combination[1] + + deckfile_aux = DeckfileAux(location="test") + source_aux = SourceAux(source=DeckfileInlineSource(content=content)) + + render_behavior = Inline( + path=None, + source=None, + config=None, + namespace=namespace, + ) + source_files = render_behavior.render( + deckfile_aux=deckfile_aux, source_aux=source_aux + ) + + self.assertEqual(len(source_files), 1) + source_file = source_files[0] + self.assertEqual(source_file.namespace, namespace or "default") + self.assertEqual(source_file.content, content)