From 59156888305899c1c67b0199d722ec942bd0d33f Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Tue, 26 Nov 2019 18:06:23 +0100 Subject: [PATCH] Add client tests --- pkg/installer/client/client_suite_test.go | 28 +++++++++ pkg/installer/client/http_test.go | 73 +++++++++++++++++++++++ pkg/installer/client/local_test.go | 66 ++++++++++++++++++++ 3 files changed, 167 insertions(+) create mode 100644 pkg/installer/client/client_suite_test.go create mode 100644 pkg/installer/client/http_test.go create mode 100644 pkg/installer/client/local_test.go diff --git a/pkg/installer/client/client_suite_test.go b/pkg/installer/client/client_suite_test.go new file mode 100644 index 00000000..597891f5 --- /dev/null +++ b/pkg/installer/client/client_suite_test.go @@ -0,0 +1,28 @@ +// Copyright © 2019 Ettore Di Giacinto +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, see . + +package client_test + +import ( + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +func TestClient(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Client Suite") +} diff --git a/pkg/installer/client/http_test.go b/pkg/installer/client/http_test.go new file mode 100644 index 00000000..ab124f32 --- /dev/null +++ b/pkg/installer/client/http_test.go @@ -0,0 +1,73 @@ +// Copyright © 2019 Ettore Di Giacinto +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, see . + +package client_test + +import ( + "io/ioutil" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + + compiler "github.com/mudler/luet/pkg/compiler" + helpers "github.com/mudler/luet/pkg/helpers" + + . "github.com/mudler/luet/pkg/installer/client" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Http client", func() { + Context("With repository", func() { + + It("Downloads single files", func() { + // setup small staticfile webserver with content + tmpdir, err := ioutil.TempDir("", "test") + Expect(err).ToNot(HaveOccurred()) + defer os.RemoveAll(tmpdir) // clean up + Expect(err).ToNot(HaveOccurred()) + ts := httptest.NewServer(http.FileServer(http.Dir(tmpdir))) + defer ts.Close() + err = ioutil.WriteFile(filepath.Join(tmpdir, "test.txt"), []byte(`test`), os.ModePerm) + Expect(err).ToNot(HaveOccurred()) + + c := NewHttpClient(RepoData{Uri: ts.URL}) + path, err := c.DownloadFile("test.txt") + Expect(err).ToNot(HaveOccurred()) + Expect(helpers.Read(path)).To(Equal("test")) + os.RemoveAll(path) + }) + + It("Downloads artifacts", func() { + // setup small staticfile webserver with content + tmpdir, err := ioutil.TempDir("", "test") + Expect(err).ToNot(HaveOccurred()) + defer os.RemoveAll(tmpdir) // clean up + Expect(err).ToNot(HaveOccurred()) + ts := httptest.NewServer(http.FileServer(http.Dir(tmpdir))) + defer ts.Close() + err = ioutil.WriteFile(filepath.Join(tmpdir, "test.txt"), []byte(`test`), os.ModePerm) + Expect(err).ToNot(HaveOccurred()) + + c := NewHttpClient(RepoData{Uri: ts.URL}) + path, err := c.DownloadArtifact(&compiler.PackageArtifact{Path: "test.txt"}) + Expect(err).ToNot(HaveOccurred()) + Expect(helpers.Read(path.GetPath())).To(Equal("test")) + os.RemoveAll(path.GetPath()) + }) + + }) +}) diff --git a/pkg/installer/client/local_test.go b/pkg/installer/client/local_test.go new file mode 100644 index 00000000..517c0718 --- /dev/null +++ b/pkg/installer/client/local_test.go @@ -0,0 +1,66 @@ +// Copyright © 2019 Ettore Di Giacinto +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, see . + +package client_test + +import ( + "io/ioutil" + "os" + "path/filepath" + + compiler "github.com/mudler/luet/pkg/compiler" + helpers "github.com/mudler/luet/pkg/helpers" + + . "github.com/mudler/luet/pkg/installer/client" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Local client", func() { + Context("With repository", func() { + It("Downloads single files", func() { + tmpdir, err := ioutil.TempDir("", "test") + Expect(err).ToNot(HaveOccurred()) + defer os.RemoveAll(tmpdir) // clean up + + // write the whole body at once + err = ioutil.WriteFile(filepath.Join(tmpdir, "test.txt"), []byte(`test`), os.ModePerm) + Expect(err).ToNot(HaveOccurred()) + + c := NewLocalClient(RepoData{Uri: tmpdir}) + path, err := c.DownloadFile("test.txt") + Expect(err).ToNot(HaveOccurred()) + Expect(helpers.Read(path)).To(Equal("test")) + os.RemoveAll(path) + }) + + It("Downloads artifacts", func() { + tmpdir, err := ioutil.TempDir("", "test") + Expect(err).ToNot(HaveOccurred()) + defer os.RemoveAll(tmpdir) // clean up + + // write the whole body at once + err = ioutil.WriteFile(filepath.Join(tmpdir, "test.txt"), []byte(`test`), os.ModePerm) + Expect(err).ToNot(HaveOccurred()) + + c := NewLocalClient(RepoData{Uri: tmpdir}) + path, err := c.DownloadArtifact(&compiler.PackageArtifact{Path: "test.txt"}) + Expect(err).ToNot(HaveOccurred()) + Expect(helpers.Read(path.GetPath())).To(Equal("test")) + os.RemoveAll(path.GetPath()) + }) + + }) +})