From 599e9ab0e8d8e63694ef6ca4c5a96a26d78e3d33 Mon Sep 17 00:00:00 2001 From: soulteary Date: Sun, 12 Jun 2022 17:15:59 +0800 Subject: [PATCH] test: improve project coverage --- apt-proxy.go | 42 +----------------- cli/cli.go | 66 ++++++++++++++-------------- apt-proxy_test.go => cli/cli_test.go | 4 +- cli/daemon.go | 65 +++++++++++++++++++++++++++ cli/daemon_test.go | 17 +++++++ 5 files changed, 118 insertions(+), 76 deletions(-) rename apt-proxy_test.go => cli/cli_test.go (94%) create mode 100644 cli/daemon.go create mode 100644 cli/daemon_test.go diff --git a/apt-proxy.go b/apt-proxy.go index df30e13..0f36062 100644 --- a/apt-proxy.go +++ b/apt-proxy.go @@ -1,48 +1,10 @@ package main import ( - "flag" - "github.com/soulteary/apt-proxy/cli" - "github.com/soulteary/apt-proxy/linux" -) - -const ( - DEFAULT_HOST = "0.0.0.0" - DEFAULT_PORT = "3142" - DEFAULT_CACHE_DIR = "./.aptcache" - DEFAULT_MIRROR = "" // "https://mirrors.tuna.tsinghua.edu.cn/ubuntu/" - DEFAULT_TYPE = linux.LINUX_DISTROS_UBUNTU - DEFAULT_DEBUG = false ) -var version string - -func parseFlags() (appFlags cli.AppFlags) { - var ( - host string - port string - types string - ) - flag.StringVar(&host, "host", DEFAULT_HOST, "the host to bind to") - flag.StringVar(&port, "port", DEFAULT_PORT, "the port to bind to") - flag.StringVar(&types, "type", DEFAULT_TYPE, "select the type of system to cache: ubuntu/debian") - flag.BoolVar(&appFlags.Debug, "debug", DEFAULT_DEBUG, "whether to output debugging logging") - flag.StringVar(&appFlags.CacheDir, "cachedir", DEFAULT_CACHE_DIR, "the dir to store cache data in") - flag.StringVar(&appFlags.Mirror, "mirror", DEFAULT_MIRROR, "the mirror for fetching packages") - flag.Parse() - - if types != linux.LINUX_DISTROS_UBUNTU && types != linux.LINUX_DISTROS_DEBIAN { - types = linux.LINUX_DISTROS_UBUNTU - } - - appFlags.Types = types - appFlags.Listen = host + ":" + port - appFlags.Version = version - - return appFlags -} - func main() { - cli.Parse(parseFlags()) + flags := cli.ParseFlags() + cli.Daemon(&flags) } diff --git a/cli/cli.go b/cli/cli.go index 2806048..dd8e490 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -1,45 +1,43 @@ package cli import ( - "log" - "net/http" + "flag" - "github.com/soulteary/apt-proxy/pkgs/httpcache" - "github.com/soulteary/apt-proxy/pkgs/httplog" - "github.com/soulteary/apt-proxy/proxy" + "github.com/soulteary/apt-proxy/linux" ) -type AppFlags struct { - Debug bool - Version string - CacheDir string - Mirror string - Types string - Listen string -} - -func Parse(appFlags AppFlags) { - log.Printf("running apt-proxy %s", appFlags.Version) - - if appFlags.Debug { - log.Printf("enable debug: true") - httpcache.DebugLogging = true - } +const ( + DEFAULT_HOST = "0.0.0.0" + DEFAULT_PORT = "3142" + DEFAULT_CACHE_DIR = "./.aptcache" + DEFAULT_MIRROR = "" // "https://mirrors.tuna.tsinghua.edu.cn/ubuntu/" + DEFAULT_TYPE = linux.LINUX_DISTROS_UBUNTU + DEFAULT_DEBUG = false +) - cache, err := httpcache.NewDiskCache(appFlags.CacheDir) - if err != nil { - log.Fatal(err) +var version string + +func ParseFlags() (appFlags AppFlags) { + var ( + host string + port string + types string + ) + flag.StringVar(&host, "host", DEFAULT_HOST, "the host to bind to") + flag.StringVar(&port, "port", DEFAULT_PORT, "the port to bind to") + flag.StringVar(&types, "type", DEFAULT_TYPE, "select the type of system to cache: ubuntu/debian") + flag.BoolVar(&appFlags.Debug, "debug", DEFAULT_DEBUG, "whether to output debugging logging") + flag.StringVar(&appFlags.CacheDir, "cachedir", DEFAULT_CACHE_DIR, "the dir to store cache data in") + flag.StringVar(&appFlags.Mirror, "mirror", DEFAULT_MIRROR, "the mirror for fetching packages") + flag.Parse() + + if types != linux.LINUX_DISTROS_UBUNTU && types != linux.LINUX_DISTROS_DEBIAN { + types = linux.LINUX_DISTROS_UBUNTU } - ap := proxy.NewAptProxyFromDefaults(appFlags.Mirror, appFlags.Types) - ap.Handler = httpcache.NewHandler(cache, ap.Handler) - - logger := httplog.NewResponseLogger(ap.Handler) - logger.DumpRequests = appFlags.Debug - logger.DumpResponses = appFlags.Debug - logger.DumpErrors = appFlags.Debug - ap.Handler = logger + appFlags.Types = types + appFlags.Listen = host + ":" + port + appFlags.Version = version - log.Printf("proxy listening on %s", appFlags.Listen) - log.Fatal(http.ListenAndServe(appFlags.Listen, ap)) + return appFlags } diff --git a/apt-proxy_test.go b/cli/cli_test.go similarity index 94% rename from apt-proxy_test.go rename to cli/cli_test.go index e15c39d..719ad71 100644 --- a/apt-proxy_test.go +++ b/cli/cli_test.go @@ -1,4 +1,4 @@ -package main +package cli import ( "os" @@ -7,7 +7,7 @@ import ( func TestParseFlags(t *testing.T) { os.Args = append(os.Args, "--type=not-support-os") - flags := parseFlags() + flags := ParseFlags() if flags.Debug != DEFAULT_DEBUG { t.Fatal("Default option `Debug` value mismatch") diff --git a/cli/daemon.go b/cli/daemon.go new file mode 100644 index 0000000..91b7dba --- /dev/null +++ b/cli/daemon.go @@ -0,0 +1,65 @@ +package cli + +import ( + "log" + "net/http" + + "github.com/soulteary/apt-proxy/pkgs/httpcache" + "github.com/soulteary/apt-proxy/pkgs/httplog" + "github.com/soulteary/apt-proxy/proxy" +) + +type AppFlags struct { + Debug bool + Version string + CacheDir string + Mirror string + Types string + Listen string +} + +func initStore(appFlags AppFlags) (cache httpcache.Cache, err error) { + cache, err = httpcache.NewDiskCache(appFlags.CacheDir) + if err != nil { + return cache, err + } + return cache, nil +} + +func initProxy(appFlags AppFlags, cache httpcache.Cache) (ap *proxy.AptProxy) { + ap = proxy.NewAptProxyFromDefaults(appFlags.Mirror, appFlags.Types) + ap.Handler = httpcache.NewHandler(cache, ap.Handler) + return ap +} + +func initLogger(appFlags AppFlags, ap *proxy.AptProxy) { + if appFlags.Debug { + log.Printf("enable debug: true") + httpcache.DebugLogging = true + } + logger := httplog.NewResponseLogger(ap.Handler) + logger.DumpRequests = appFlags.Debug + logger.DumpResponses = appFlags.Debug + logger.DumpErrors = appFlags.Debug + ap.Handler = logger +} + +func StartServer(appFlags *AppFlags, ap *proxy.AptProxy) { + log.Printf("proxy listening on %s", appFlags.Listen) + log.Fatal(http.ListenAndServe(appFlags.Listen, ap)) +} + +func Daemon(appFlags *AppFlags) { + log.Printf("running apt-proxy %s", appFlags.Version) + + cache, err := initStore(*appFlags) + if err != nil { + log.Fatal(err) + } + + ap := initProxy(*appFlags, cache) + + initLogger(*appFlags, ap) + + StartServer(&*appFlags, ap) +} diff --git a/cli/daemon_test.go b/cli/daemon_test.go new file mode 100644 index 0000000..a7125c0 --- /dev/null +++ b/cli/daemon_test.go @@ -0,0 +1,17 @@ +package cli + +import ( + "testing" +) + +func TestInit(t *testing.T) { + flags := ParseFlags() + + cache, err := initStore(flags) + if err != nil { + t.Fatal("Init Store Failed") + } + + ap := initProxy(flags, cache) + initLogger(flags, ap) +}