#!/usr/bin/env lua

-- Copyright (c) Meta Platforms, Inc. and affiliates.
--
-- This source code is licensed under the MIT license found in the
-- LICENSE file in the root directory of this source tree.

local update_snmpd_conf = dofile("/usr/sbin/update_snmpd_conf")

local lu = require "luaunit"

TestMain = {}

--- Basic functionality checks.
function TestMain:testBasicFunctionality()
  lu.assertIsFunction(update_snmpd_conf.buildSnmpdConfig)
end

--- Test full execution with good config.
function TestMain:testGoodConfig()
  local config = {
    snmpConfig = {
      location = "Menlo Park, CA",
      contact = "Facebook",
      snmpV2C = {
        publicConfig = {
          community = "public",
          source = "fd00::/8"
        }
      },
      snmpV3 = {
        authPrivConfig = {
          username = "tg",
          authPassphrase = "FbTestAuth123",
          authType = "MD5",
          privPassphrase = "FbTestPriv123",
          privProtocol = "DES"
        }
      }
    }
  }

  local output = update_snmpd_conf.buildSnmpdConfig(config)
  lu.assertEquals(output, [[
# NOTE: This file is automatically generated. DO NOT EDIT MANUALLY!

# listen on all interfaces
agentAddress udp6:161

# enable agentx socket
master agentx
syslocation Menlo Park, CA
syscontact Facebook
# snmp v2c config - publicConfig
rocommunity6 public fd00::/8
# snmp v3 config - authPrivConfig
rouser tg
createUser tg MD5 "FbTestAuth123" DES "FbTestPriv123"
]])
end

--- Test execution with empty config.
function TestMain:testEmptyConfig()
  local output = update_snmpd_conf.buildSnmpdConfig({})
  lu.assertEquals(output, [[
# NOTE: This file is automatically generated. DO NOT EDIT MANUALLY!

# listen on all interfaces
agentAddress udp6:161

# enable agentx socket
master agentx
syslocation Undefined
syscontact Undefined
]])
end

--- Test execution with required options set in snmp v2c config
function TestMain:testSnmpV2cPartialOptions()
  local config = {
    snmpConfig = {
      location = "Menlo Park, CA",
      contact = "Facebook",
      snmpV2C = {
        publicConfig = {
          community = "public"
        }
      }
    }
  }

  local output = update_snmpd_conf.buildSnmpdConfig(config)
  lu.assertEquals(output, [[
# NOTE: This file is automatically generated. DO NOT EDIT MANUALLY!

# listen on all interfaces
agentAddress udp6:161

# enable agentx socket
master agentx
syslocation Menlo Park, CA
syscontact Facebook
# snmp v2c config - publicConfig
rocommunity6 public
]])
end

--- Test execution with required options set in snmp v3 config
function TestMain:testSnmpV3PartialOptions()
  local config = {
    snmpConfig = {
      location = "Menlo Park, CA",
      contact = "Facebook",
      snmpV3 = {
        authNoPrivConfig = {
          username = "tg",
          authPassphrase = "FbTestAuth123",
          authType = "MD5",
        }
      }
    }
  }

  local output = update_snmpd_conf.buildSnmpdConfig(config)
  lu.assertEquals(output, [[
# NOTE: This file is automatically generated. DO NOT EDIT MANUALLY!

# listen on all interfaces
agentAddress udp6:161

# enable agentx socket
master agentx
syslocation Menlo Park, CA
syscontact Facebook
# snmp v3 config - authNoPrivConfig
rouser tg
createUser tg MD5 "FbTestAuth123"
]])
end

os.exit(lu.LuaUnit.run("-v"))
